Get a value of object field inside fields_for loop

Nazar Hussain picture Nazar Hussain · Dec 26, 2010 · Viewed 16.8k times · Source

In the following scenario, I need to check the value of the object property in the fields_for loop.

<%= f.semantic_fields_for :review_details do |rd| %>
  <%= rd.input :review_criteria_id, :as=>:hidden %>
<% end %>

As in the loop, :review_criteria_id is rendered as hidden field, but I have a scenario, where I have to print some more information if it is a specific criteria. How can I get the value of review_criteria_id in the loop. I used:

rd.review_criteria_id

But since rd is the formtastic object, so I couldn't get the value of :review_crieteria_id.

Answer

Scott picture Scott · Dec 26, 2010

Formtastic adds additional features to the Rails code, but doesn't take away existing functionality so the following should work for you:

rd.object.review_criteria_id

'object' can be used in plain Rails form helpers to access the underlying bound object, and Formtastic honours this convention.