Cameron Bass Cameron Bass - 29 days ago 12
Ruby Question

Changing the way params comes in from the form - Rails

Right now I'm using a hidden field tag to get the plan_id. But when I use I binding.pry it's nested in the accounts hash. In order to get to the plan_id I have to do this

params[:accounts][:plan_id]
when I simply want to do this
params[:plan_id]
. How can I achieve this? Here is my code.

<label>
<%= f.hidden_field :plan_id, value: 1 %>
<%= f.submit "Select", class: "button-big reverse-blue" %>
</label>


Here is my params look like

{ "account"=>{"name"=>"something", "plan_id"=>"1"}, "stripe_card_token"=>"", "card_number"=>"", "card_holder"=>"", "controller"=>"accounts", "action"=>"create"}


I want them to look like this

{"account"=>{"name"=>"something"}, "plan_id"=>"1", "stripe_card_token"=>"", "card_number"=>"", "card_holder"=>"", "controller"=>"accounts", "action"=>"create"}


Again. I'd like to access the plan Id by simply typing this
params[:plan_id]

Answer

You could use hidden_field_tag

<%= hidden_field_tag :plan_id, 1 %>
Comments