I'm working with Ruby on Rails and I have a simple form gem that is like this:
Code problem
<%= simple_form_for [:admins, @calendar_entry],
url: toggle_visibility_admins_calendar_entry_path(calendar_entry),
method: :patch,
remote: true do |f| %>
<%= f.input_field :visible,
id: 'switch-sm-#{calendar_entry.id.to_s}',
class: 'switch',
type: 'checkbox' %>
<% end %>
And it gets the following output in HTML (check the id attribute in input):
<form novalidate="novalidate" class="simple_form new_calendar_entry" id="new_calendar_entry" action="/admins/calendar_entries/35/toggle_visibility" accept-charset="UTF-8" data-remote="true" method="post">
<input id="switch-sm-#{calendar_entry.id.to_s}" class="boolean optional switch" type="checkbox" value="1" name="calendar_entry[visible]">
</form>
Ignoring the remote: true
and all the attributes in the simple form, the real problem is the interpolation in the id attribute, for any reason I don't get the data of the variable, however, I can do this way:
Bad code by standards
<%= simple_form_for [:admins, @calendar_entry],
url: toggle_visibility_admins_calendar_entry_path(calendar_entry),
method: :patch,
remote: true do |f| %>
<%= f.input_field :visible,
id: 'switch-sm-'+calendar_entry.id.to_s,
class: 'switch',
type: 'checkbox' %>
<% end %>
And yes, this second code works, but I want to do it by the first option, by interpolation, so my question is:
You need to use double quotes when performing string interpolation and do not need to call to_s
(it will automatically be converted to a string)....
id: "switch-sm-#{calendar_entry.id}"