Martin Stabenfeldt
2006-May-09 11:39 UTC
[Rails] User.new doesn´t capture all of the parameters
Hi, I have a page where I can add new users to the database, but it only sees whats in the name and passord field. add_user.rhtml: <%= form_tag %> <%= form_tag :action => ''add_user'' %> <td>User name:</td> <td><%= text_field("user", "name") %></td> </tr> <tr> <td>Password:</td> <td><%= password_field("user", "password") %></td> </tr> <tr> <td>Company:</td> <td><%= text_field("user", "company") %></td <tr> The database looks like this: create table users ( id int not null auto_increment, name varchar(100) not null, company varchar(100) not null, ownnumber int not null, sendt int not null, count int not null, msg_limit int not null, access_level varchar(50) not null, hashed_password char(40) not null, primary key (id) ); login_controller.rb: def add_user if request.get? @user = User.new else @user.company = (params[:user][:company]) # Tried first without this line, since I didn?t see any for name and password. # I hoped the one below would capture all the fields. @user = User.new(params[:user]) # How come this doesn''t manage to save whats in "company", "ownnumber" etc. ? puts "DEBUG: add_user() user is #{@user.name} from the company #{@user.company}, count is #{@user.count}" # this is empty if @user.save redirect_to(:controller => "numbers", :action => "index") end end end Debug info: Parameters: {"user"=>{"name"=>"test3", "company"=>"family", "ownnumber"=>"555983781", "count"=>"10", "access_level"=>"", "password"=>"SECRET"}} How come @user = User.new(params[:user] doesn?t saves whats in "company", "count" etc. ? I?ll be very grateful to everyone that comes with suggestions or hint on what I?m doing wrong! Kind regards, Martin Stabenfeldt
Philip Ross
2006-May-09 21:32 UTC
[Rails] Re: User.new doesn´t capture all of the parameters
Martin Stabenfeldt wrote:> Debug info: > Parameters: {"user"=>{"name"=>"test3", "company"=>"family", > "ownnumber"=>"555983781", "count"=>"10", "access_level"=>"", > "password"=>"SECRET"}} > > How come @user = User.new(params[:user] doesn?t saves whats in > "company", "count" etc. ?Are you using attr_accessible or attr_protected in your User model? If you are, you''ll need to adjust them to allow company, count, etc to be mass-assigned. See the API docs for more information: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000874 -- Philip Ross http://tzinfo.rubyforge.org/ -- DST-aware timezone library for Ruby
Martin Stabenfeldt
2006-May-10 08:57 UTC
Re: [Rails] Re: User.new doesn´t capture all of the parameters
Hi, On 5/9/06, Philip Ross <phil.ross@gmail.com> wrote:> > Martin Stabenfeldt wrote: > > Debug info: > > Parameters: {"user"=>{"name"=>"test3", "company"=>"family", > > "ownnumber"=>"555983781", "count"=>"10", "access_level"=>"", > > "password"=>"SECRET"}} > > > > How come @user = User.new(params[:user] doesn?t saves whats in > > "company", "count" etc. ? > > Are you using attr_accessible or attr_protected in your User model? If > you are, you''ll need to adjust them to allow company, count, etc to be > mass-assigned. > > See the API docs for more information: > > http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000874 > > Thanks, that got me on track again! :)-- Best regards, Martin Stabenfeldt -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060510/68697d5e/attachment.html