0

I'm following this SE question to put a form in a bootstrap modal with rails. The person who answered the question states: "Make sure that you have a unique id or class for every modal body.". So I am trying to put a unique id number in my link_to:

<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "<%= post.id %>-my-modal" %>

But this is causing an error. If I take out <%= post.id %> I do not have an error, but the modal behavior does not work.

How can I add the post.id with embedded ruby to the link to?

Community
  • 1
  • 1
user2884789
  • 373
  • 1
  • 8
  • 21

3 Answers3

3

You have to write it like this:

<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>
Sahil
  • 3,338
  • 1
  • 21
  • 43
1

Once you open a <% ... %> tag, you are writing ruby code. What this means is that you can't nest <% ... %> tags inside another <% ... %> tag because these tags aren't ruby syntax.

Inside the tag, to do string interpolation, use normal ruby methods:

<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>
0

Only mistake is putting another (ruby) tag <% %> inside the same tag.

In erb (embedded ruby) one <%= %> tag cannot have another tag inside.

This code result a syntax error.

<%= link_to "Edit", .... "data-target" => "<%= post.id %>-my-modal" %>

If you plan to output/render data inside the tag and quoted as string, used #{put_data_hare}

Output like:

<%= link_to "Edit", .... "data-target" => "#{post.id}-my-modal" %>

In other case, you can do like:

post.id.to_s + "-my-modal"
aldrien.h
  • 3,437
  • 2
  • 30
  • 52