Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a value of object field inside fields_for loop

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.

like image 294
Nazar Hussain Avatar asked Dec 26 '10 20:12

Nazar Hussain


2 Answers

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.

like image 119
Scott Avatar answered Nov 16 '22 01:11

Scott


I got it, I can use

rd.object.review_criteria_id

object is the default wrapper object for the fields_for loop.

like image 25
Nazar Hussain Avatar answered Nov 16 '22 01:11

Nazar Hussain