Rails: How do I use hidden_field in a form_for?
Ref hidden_field or hidden_field_tag
<% form_for(:request, :url => requests_path) do |f| %>
<div class="actions">
<%= f.hidden_field :some_column %>
<%= hidden_field_tag 'selected', 'none' %>
<%= f.submit e %>
</div>
<% end %>
then in controller
params[:selected]="none"
params[:request][:some_column] = request.some_column
Note when you used
<%= f.hidden_field :some_column %>
it change to html
<input type="hidden" id="request_some_column" name="request[some_column]" value="#{@request.some_column}" />
and when you used
<%= hidden_field_tag 'selected', 'none' %>
it change to html
<input id="selected" name="selected" type="hidden" value="none"/>
rails simple_form - hidden field - create?
try this
= f.input :title, :as => :hidden, :input_html => { :value => "some value" }
How to use hidden field tag in rails
Try doing:
<%= f.hidden_field :client_id, value: @assessment.client_id %>
Since, as in your prior question, you are setting @assessment.client_id in your new
action.
How do I put a default string value into a hidden field?
<%=f.hidden_field :role, :value => 'Reader', readonly: true%> or
<%=f.text_field :role, :value => 'Reader', readonly: true%>
By giving " readonly: true " the value could not be editable in the form
Rails Hidden Field Not Saving Params
@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 %>
Related Topics
Ruby on Rails, Two Models in One Form
Reading the First Line of a File in Ruby
Rails - How to Check Developer Mode or Production Mode in Code
What Is the Easiest Way I Can Create a 'Beep' Sound from a Ruby Program
Convert Datetime String to Utc in Rails
How to Log the Entire Trace Back of a Ruby Exception Using the Default Rails Logger
Rails 5.2 Active Storage Add Custom Attributes
How to Access a Ruby Module Method
Better Way to Turn a Ruby Class into a Module Than Using Refinements
Differencebetween Methods and Attributes in Ruby
Redirect the "Puts" Command Output to a Log File
Rails: Ensure Only One Boolean Field Is Set to True at a Time
Loop Through Activerecord::Associations::Collectionproxy with Each
Ruby Request to Https - "In 'Read_Nonblock': Connection Reset by Peer (Errno::Econnreset)"