Fernando
Fernando

Reputation: 7895

rails post parameters show but not inserted

I'm creating a small app in Rails 3.2.13, using bootstrap-sass gem. I have the following situation:

My 'sign_up' controller has and 'index' action that creates a user like this:

#sign_up controller
def index
    @user = User.new
end

Now, in my index.html.erb i have the form:

# sign_up controller => index.html.erb
<%= form_for @user, :html => {:class => 'form-horizontal'} do |f| %>    
<div class = 'control-group'>
    <%= f.label :username, :html => {:class => 'control-label'} %>      
    <%= text_field_tag :username, nil, :placeholder => 'Username' %>
</div>
<div class = 'control-group'>
    <%= f.label :email, :html => {:class => 'control-label'} %>     
    <%= email_field_tag :email, nil, :placeholder => 'Email' %>
</div>
<div class = 'control-group'>
    <%= f.label :password, :html => {:class => 'control-label'} %>
    <%= password_field_tag :password, nil, :placeholder => 'Password' %>
</div>
<div class = 'control-group'>
    <%= f.label :confirm_password, :html => {:class => 'control-label'} %>
    <%= password_field_tag :confirm_password, nil, :placeholder => 'Confirm' %>
</div>
<%= f.submit 'Create', :class => 'btn btn-primary'%>

This will POST to users_controller, calling the create action:

 # users controller
 def create
  @user = User.new(params[:user])

   respond_to do |format|
    if @user.save
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render json: @user, status: :created, location: @user }
    else
      format.html { render action: "new" }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

The problem is: the @user is saved, but the parameters are lost! In the console log:

Started POST "/users" for 127.0.0.1 at 2013-05-29 12:14:47 -0300

Processing by UsersController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"wI5sNQV/lkn8GtYv5YkRLZ6HzxPg5MtoVXuarLulv30=", "username"=>"nonickname", "email"=>"[email protected]", "password"=>"[FILTERED]", "confirm_password"=>"[FILTERED]", "commit"=>"Create"} (0.1ms) begin transaction SQL (0.9ms) INSERT INTO "users" ("admin", "created_at", "email", "password_hash", "password_salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?) [["admin", nil], ["created_at", Wed, 29 May 2013 15:14:47 UTC +00:00], ["email", nil], ["password_hash", nil], ["password_salt", nil], ["updated_at", Wed, 29 May 2013 15:14:47 UTC +00:00], ["username", nil]] (148.9ms) commit transaction...

Why that happens, and how can i fix this? I know the form should be a users_controller domain(i plan to fix that), but it doesn't seem to be the problem!

Thanks for any help!

Upvotes: 1

Views: 787

Answers (2)

rmagnum2002
rmagnum2002

Reputation: 11421

I'll try to help you here as much as I can. Have a look at the console when I create a comment:

Started POST "/companies/1/comments" for 127.0.0.1 at 2013-05-29 20:06:02 +0300
Processing by CommentsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"+J+ewToARuBybidK79V/3cFdX6=", "comment"=>{"content"=>"Lorem ipsum..."}, "commit"=>"Add Comments", "company_id"=>"1"}
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `comments` (`commentable_id`, `commentable_type`, `content`, `created_at`, `updated_at`, `user_id`) VALUES (1, 'Company', 'Lorem ipsum...', '2013-05-29 17:06:02', '2013-05-29 17:06:02', 2)
   (42.7ms)  COMMIT

now when I say on conrtoller @comment = Comment.new(params[:comment]) means it will look for this line "comment"=>{"content"=>"Lorem ipsum dolor sit amet, ..."}

that means params[:comment] in my case is content, parameters are being sent as a hash and in your case you must have something like:

"user"=>{authenticity_token"=>"wI5sNQV/lkn8GtYv5YkRLZ6Hzx=", "username"=>"nonickname", "email"=>"[email protected]", "password"=>"[FILTERED]", "confirm_password"=>"[FILTERED]"}`

in your console logs. I've looked a few times in your console logs and didn't see the user there and you can't call @user = User.new(params[:user]) as long as it is missing.

about the form, if you are using:

<%= form_for @user, :html => {:class => 'form-horizontal'} do |f| %>

you could use f to build your fields and write less this way

<%= form_for @user, :html => {:class => 'form-horizontal'} do |f| %>    
<div class = 'control-group'>
    <%= f.label :username, :html => {:class => 'control-label'} %>      
    <%= f.text_field :username, :placeholder => 'Username' %>
</div>
<div class = 'control-group'>
    <%= f.label :email, :html => {:class => 'control-label'} %>     
    <%= f.email_field :email, :placeholder => 'Email' %>
</div>
<div class = 'control-group'>
    <%= f.label :password, :html => {:class => 'control-label'} %>
    <%= f.password_field :passwor, :placeholder => 'Password' %>
</div>
<div class = 'control-group'>
    <%= f.label :confirm_password, :html => {:class => 'control-label'} %>
    <%= f.password_field :confirm_password, :placeholder => 'Confirm' %>
</div>
<%= f.submit 'Create', :class => 'btn btn-primary'%>

Upvotes: 1

Fernando
Fernando

Reputation: 7895

Ok, it's solved!

The problem was the bootstrap form, i need to set the name attribute:

  ...
  <div class = 'control-group'>
    <%= f.label :email, :html => {:class => 'control-label'} %>     
    <%= email_field_tag :email, nil, :name => 'user[email]', :placeholder => 'Email' %>
</div>
  ...

Thanks!

Upvotes: 0

Related Questions