Search code examples
ruby-on-railsrubyformtastic

Form Helper: Fields_for - display image


Is it possible to take from a form builder text_field, only the value of it? For example:

<% f.text_field :name %>

Generates:

<input type="text" value="ddd" size="30" name="girl[name]" id="girl_name">

How can I grab the value "ddd"?

One way is by storing the generated html and take what I wanted with regex. Other way is by creating a custom form builder (like Ryan did in railscasts episode 311) but I found both of the solutions a bit extreme, isn't it? Is there any other simpler solution?

My real problem is that I need inside a fields_for to grab the value of a :photo and add it in an image_tag so I can display the images with the loop of the builder.


Solution

  • You can get the current form object with f.object, so in your example you could do this:

    <%= f.object.name %>