When using the form_for helper with an html.erb page, alot of the work is abstracted for you, so something like
f.label :book
gets turned into an html label. I want to fill in a text field with a string, so that I can take the values associated with several symbols, form them into a string, and put that string into the :value key.
<%= form_for(@backpack) do |f| %>
<ul>
<% @backpack.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
<p>
<%= f.label :calculator %><br/>
<%= f.text_field :calculator %>
</p>
<p>
<%= f.label :lunchbox %><br/>
<%= f.text_field :lunchbox %>
</p>
<%= f.label :books %><br/>
</p>
<%= f.fields_for :books do |g| %>
<p>
<%= g.text_field :book, :value => MY_STRING %>
</p>
<% end %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
MY_STRING = :title by :author
So, if I left g.text_field alone with :book, it would automatically fill in the string associated with that symbol. How can I implement that in my custom way? How exactly do symbols reference the actual data in my nested form?
I should also mention I have the
accepts_nested_attributes_for :books
attribute in my backpack controller, so I have access to the second model/database
A symbol is just a symbol. No direct connection to the object. But you can use the book object attached to the form builder to create a string describing the book:
Even better, you can write a helper to handle this for you:
And use it like so: