How do I use form_for?
I'm trying to create a form to create a user. Currently I have
- form_for @user do |f|
= "Username"
%br
= f.text_field :username
%br
= "Password"
%br
= f.text_field :password
%br
= "Confirm Pa开发者_高级运维ssword"
%br
= f.text_field :password_confirmation
%br
= "Email"
%br
= f.text_field :email
%br
= f.submit "Submit app"
I keep getting the error undefined method 'password' for #<User:0x8e20750>
User does have a method password=, but because I'm encrypting the password before I store it, there is no password method. Anyone have a work around or a fix?
You should post your model as well, but I am guessing, that what you need is to add:
attr_accessor :password
On a side note, is there a reason you are doing your own Authentication logic? Maybe you should try complete solutions like Authlogic or Devise?
Also, please take a look on Formtastic Gem. It really helps to create forms.
I agree with mdrozdziel that you might want to give Authlogic (not tried Devise myself) a shot.
When using form_for f.text_field :some_field is like saying text_field :model, :some_field. So :some_field needs to a field in your db or you need getter and setter methods.
as mdrozdziel said
attr_accessor :password
or you can define getter and setter methods in your model if you want more control.
#in your model
def password
# return something
end
def password=(pwd)
# set something
end
精彩评论