35

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.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Nazar Hussain
  • 5,102
  • 6
  • 40
  • 67

2 Answers2

74

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.

Scott
  • 17,127
  • 5
  • 53
  • 64
  • I'd add that you can find this by debugging the view. Adding a debug statement (in this case byebug) `<% byebug %>` inside the `fields_for` block gives a person access to the builder object itself. You can then call `methods` on said class and start investigating. – Tass Jul 06 '16 at 21:49
12

I got it, I can use

rd.object.review_criteria_id

object is the default wrapper object for the fields_for loop.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Nazar Hussain
  • 5,102
  • 6
  • 40
  • 67