Reputation: 1437
I'm using an if/else statement in my form to set some fields invisibly if a @goal
variable is present, but to allow them to set it using a dropdown if it isn't.
My HTML/ERB looks like this:
<% if @goal %>
<%= hidden_field :area_id, value: @goal.area_id %>
<%= hidden_field :goal_id, value: @goal.id %>
<% else %>
<section id="milestone-associations">
<h4 class="color-areafont-weight-bold border-bottom-area text-center pb-1">
Associations
</h4>
<div class="row">
<% area_array = [] %>
<% Area.where(user_id: current_user.id, archived: false).each do |area| %>
<% area_array << [area.name, area.id] %>
<% end %>
<div class="col-sm-6 form-group">
<label for="orangeForm-area" class="mb-0">Which key area is this in?</label>
<%= f.input :area_id, label: false, prompt: "Select Key Area", input_html: { class: 'mdb-select md-form mt-0', style: "margin-top: 0px" }, collection: area_array %>
</div>
<% goals_array = [] %>
<% Goal.where(user_id: current_user.id, archived: false).each do |goal| %>
<% goals_array << [goal.area.name + " :: " + goal.name, goal.id] %>
<% end %>
<div class="col-sm-6 form-group">
<label for="orangeForm-goal" class="mb-0">Is this associated with a goal?</label>
<%= f.input :goal_id, label: false, prompt: "Select Goal", input_html: { class: 'mdb-select md-form mt-0', style: "margin-top: 0px" }, collection: goals_array.sort %>
</div>
</div> <!-- row -->
</section>
<% end %>
The if/else is working, but something in the hidden fields isn't working properly. Here's what it shows up as in the server:
Processing by MilestonesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"nehWSkm4CnLsnb7aedchtwLixxYljCZ9AwdQlh82Rnk/QCYfWCb7tP0W7x8CaI1cXwjbYF6KF0Zi6LpW8gH2qg==", "milestone"=>{"name"=>"Create More Stuff", "description"=>"", "official_start_date"=>"26 November, 2020", "external_due_date"=>"", "notes"=>""}, "area_id"=>{"{:value=>3}"=>""}, "goal_id"=>{"{:value=>6}"=>""}, "commit"=>"Create Milestone"}
First question: Why are the goal_id
and area_id
outside the {} for the new milestone?
Second question: Why are they showing up as {:value=>3}? I have tried removing the "value: " from my erb, but it yields this weirdness instead:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"X1U/ddcpAh3Jcwj1c/B3tETStmfWk70FeUaHkdRzOlH9/U8gxrfz29j4WTAIT9tfGTiqEa2VjD4YqW1ROUSKgg==", "milestone"=>{"name"=>"Testy", "description"=>"", "official_start_date"=>"17 November, 2020", "external_due_date"=>"", "notes"=>""}, "area_id"=>{"3"=>""}, "goal_id"=>{"2"=>""}, "commit"=>"Create Milestone"}
How can I get this to save properly?
Upvotes: 0
Views: 486
Reputation: 417
@axel is right, you need to attach your hidden_field
call to your form instance, f
in this case. If you want the hidden inputs to build properly but not be a part of the milestone you should be using hidden_field_tag
instead of hidden_field
.
So either
<%= f.hidden_field :area_id %>
<%= f.hidden_field :goal_id %>
or
<%= hidden_field_tag :area_id, value: @goal.area_id %>
<%= hidden_field_tag :goal_id, value: @goal.id %>
Upvotes: 3