0

I have in rails the following form in a view

<%= form_for (@account) do |f| %>
<%= f.label :comments,"Comments" %>
<%=f.text_area :comments %>
<%= f.submit "Confirm",:name=>"conf" %>
<%= f.submit "Reject" %>
<% end %>

When I submit the form I get the following hash in the log before the update of the database

Started PATCH "/accounts/12" for 127.0.0.1 at 2015-08-13 21:31:18 +0200
Processing by UseractionsController#answer_with_comments as HTML
Parameters: {"utf8"=>"✓", "account"=>{"comments"=>"mycomments"}, "conf"=>"Confirm", "id"=>"12"}

I am trying to access the input in the comments text area in the controller. I tried

params[:account][:comments]

but it does not seem to work. Could anyone give me the appropriate syntax? Thanks.

EDIT

This is my controller code. Right now the if loop return false and nothing is added to the database even though there is something submitted ("mycomments" see above in the param nested hash)

if params[:bankaccount][:comments]
  @bankaccount.update_attribute(:comments, params[:bankaccount][:comments])
end
Bastien
  • 596
  • 1
  • 11
  • 30
  • please explain what happens when you try to do this `params[:account][:comments]` wont you get `mycomments` as output. share your action controller code as well. – Athar Aug 13 '15 at 19:51
  • 1
    you might want to try `params['account']['comments']` (Hash's keys as Strings instead of Symbols) – MrYoshiji Aug 13 '15 at 19:57

2 Answers2

0

It is only the appropriate syntax for your view. It assumes that you have content field on your Comment model.

<%= form_for (@account) do |f| %>
  <%= f.label :comments,"Comments" %>
  <%= f.fields_for :comments do |ff| %>
    <%= ff.text_field :content %> 
  <% end %>
  <%= f.submit "Confirm",:name=>"conf" %>
  <%= f.submit "Reject" %>
<% end %>

You also will have to declare nested attributes in your Account model and your params hash should be different.

You should watch these two Railscasts part 1 and part 2 to learn more about nested attributes.

0

Since you mention strong parameters as a tag you probably want to build this a bit differently.

private
def account_params
  #the permit method might need to be altered depending on your model and view
  params.require(:account).permit(:comments) 
end

Somewhere else in your controller you would then do:

@bankaccount.update_attributes(account_params)

Please take a read: http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters

danielrsmith
  • 4,050
  • 3
  • 26
  • 32