Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert variable values into HTML tags in ERB templates?

I have a partial like:

<% office.map do |o| %>
   <input id='city' name='company[company_office][0][city]' value=.... type='text' />
<% end %>

How can I insert a value like o.office into an attribute? value="#{o.office}" does not work.

like image 784
maxfry Avatar asked May 30 '11 04:05

maxfry


2 Answers

<% office.map do |o| %>
   <input id='city' name='company[company_office][0][city]' value='<%= o.office %>' type='text' />
<% end %>

or you could use the form helpers for that

like image 138
corroded Avatar answered Oct 03 '22 23:10

corroded


Use embedded ruby(erb) tags,

<%= o.office %>

The only time you'd use #{o.office} is when you're not using erb. In a helper method for example and you want to use your ruby in a string. But when you're in an html.erb file, you have to use the erb tags.

like image 29
ardavis Avatar answered Oct 04 '22 01:10

ardavis