Hey Group,
I''m not sure where I''m going wrong here. I''m pretty
new to RoR. I want
to update a couple fields in a specific record, so I get the information
from my form using my "Login" controller:
def confirmation
@user = User.new(params[:user])
confirm_change = @user.update_user
if confirm_change
redirect_to(:action => "index")
flash[:notice] = "Your password has been changed."
end
end
As you can see, I pass the parameters to my "User" model and then call
the "update_user" method:
def update_user
User.change_password(self.id, self.password)
end
I am misunderstanding something here, because "self.id" does not get
the
ID from the parameters. What am I doing wrong?
Thanks for the help.
Clint
Hi Clint, I believe the reason the ID is not being set is because it will be determined at the time of the save. This is because the Rails way is to have the IDs be auto_incremented primary keys. If all you need to do is update a single attribute, you could try the update_attribute method: http://rails.rubyonrails.com/classes/ActiveRecord/Base.html#M000714 If you are updating multiple attributes, you could use the update_attributes method. You may also want to check out the Salted Login Generator: http://wiki.rubyonrails.com/rails/pages/SaltedHashLoginGenerator Even if you don''t use it, you could at least look at the code for examples. One thing that seems missing from your code is the hashing of the password. Good luck, Tom On 10/6/05, Clint Pidlubny <clint-DOvxo+vduAZWk0Htik3J/w@public.gmane.org> wrote:> Hey Group, > > I''m not sure where I''m going wrong here. I''m pretty new to RoR. I want > to update a couple fields in a specific record, so I get the information > from my form using my "Login" controller: > > def confirmation > @user = User.new(params[:user]) > confirm_change = @user.update_user > > if confirm_change > redirect_to(:action => "index") > flash[:notice] = "Your password has been changed." > end > end > > As you can see, I pass the parameters to my "User" model and then call > the "update_user" method: > > def update_user > User.change_password(self.id, self.password) > end > > I am misunderstanding something here, because "self.id" does not get the > ID from the parameters. What am I doing wrong? > > Thanks for the help. > Clint > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hey Tom,
Thanks for the tip. I searched out help on the RoR irc last night and
through some help took a different approach to the update.
Here''s my new Confirmation method, in case anyone finds it useful. But
I
suspect it''s pretty basic for most.
def confirmation
if request.get?
@user = User.new
else
id = session[:user_id]
@user = User.find(id)
@user.password = params[:user][:password]
@user.password_confirmation =
params[:user][:password_confirmation]
@user.confirmed_on = Time.now
if @user.save
redirect_to(:action => "index")
flash[:notice] = "Your password has been changed."
else
flash[:notice] = "Passwords did not match."
end
end
end
Clint
Tom Davies wrote:
>Hi Clint,
>
>I believe the reason the ID is not being set is because it will be
>determined at the time of the save. This is because the Rails way is
>to have the IDs be auto_incremented primary keys.
>
>If all you need to do is update a single attribute, you could try the
>update_attribute method:
>
>http://rails.rubyonrails.com/classes/ActiveRecord/Base.html#M000714
>
>If you are updating multiple attributes, you could use the
>update_attributes method.
>
>You may also want to check out the Salted Login Generator:
>
>http://wiki.rubyonrails.com/rails/pages/SaltedHashLoginGenerator
>
>Even if you don''t use it, you could at least look at the code for
>examples. One thing that seems missing from your code is the hashing
>of the password.
>
>Good luck,
>Tom
>
>On 10/6/05, Clint Pidlubny
<clint-DOvxo+vduAZWk0Htik3J/w@public.gmane.org> wrote:
>
>
>>Hey Group,
>>
>>I''m not sure where I''m going wrong here. I''m
pretty new to RoR. I want
>>to update a couple fields in a specific record, so I get the information
>>from my form using my "Login" controller:
>>
>> def confirmation
>> @user = User.new(params[:user])
>> confirm_change = @user.update_user
>>
>> if confirm_change
>> redirect_to(:action => "index")
>> flash[:notice] = "Your password has been
changed."
>> end
>> end
>>
>>As you can see, I pass the parameters to my "User" model and
then call
>>the "update_user" method:
>>
>> def update_user
>> User.change_password(self.id, self.password)
>> end
>>
>>I am misunderstanding something here, because "self.id" does
not get the
>>ID from the parameters. What am I doing wrong?
>>
>>Thanks for the help.
>>Clint
>>
>>_______________________________________________
>>Rails mailing list
>>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>>http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
>_______________________________________________
>Rails mailing list
>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
>
--
Clint Pidlubny
clint-DOvxo+vduAZWk0Htik3J/w@public.gmane.org
612.590.8343