Search code examples
ruby-on-railssimple-form

Why am I getting params value as an hash for simple_form input?


My simple_form code

<%= simple_form_for @post do |f| %>
  <%= f.input :post,:as => :text %>
  <%= f.button :submit %>
<% end %>

Where post is a Post.new and :post is a column of Post.

Since my create method was not saving value as expected , I just printed the value of :post params in create.html.erb and the output was {"post"=>"My sample post"}.

create.html.erb

@post = params[:post]

Why does params[:post] give {"post"=>"My sample post"} instead of My sample post?


Solution

  • You are expecting wrong output. The output is right.

    Because in rails params structure is like below:

    params[:model_name][:attribute_name]
    

    So if you want to print your expected result then try use params[:post][:post] instead of params[:post]