In an edit.html.erb file I have the code
<%= form_for @submission do |f| %>
<%= f.fields_for :submitted_answers do |answer| %>
<%= answer.label :question_id %>
<%= answer.number_field :question_id %>
<% end %>
<% end %>
This shows me a number field that has the current value of question_id and allows me to change it. Now I just want to display that value and not let anyone change it. How would I get that value?
If I say
<% @question = :question_id %>
question just equals a string: “question_id”
You seem to be mistaking what’s going on here. A symbol is an object that’s similar to a string, but stored differently in memory and with a different syntax. When you pass a symbol to many methods in Ruby, they’re used as references to methods. In this case, the
:question_idsymbol being passed as an argument to thenumber_fieldhelper method, it’s subsequently being called on the form object (that is, what is likely to be an ActiveRecord model).Long story short,
answer, a form builder, gets the question id from the model you passed into yourform_forcall. To display the value, you can do the same thing by calling the method directly, i.e.:However, since you’re iterating over these using
fields_for, you can instead get to the object the form builder is referencing by callingobject: