Question

I'm using rails 4.

Is there any way to add post.id to link_to href?

<%=  link_to_modal "Demo Form", "#demo-form-"+post.id, :class=>"button" %>
and
<%=  link_to_modal "Demo Form", "#demo-form-"(post.id), :class=>"button" %>

Doesn't work.

Was it helpful?

Solution

Try this:

<%= link_to_modal "Demo Form", "#demo-form-#{post.id}", :class=>"button" %>

OTHER TIPS

Adding this answer to point out the error and how you could have fixed it with your existing code.

The only problem with this line:

<%=  link_to_modal "Demo Form", "#demo-form-"+post.id, :class=>"button" %>

is that you're trying to concat a Fixnum with string which should have generated can't convert Fixnum into String error.

You could have used post.id.to_s to fix what you already had as:

<%=  link_to_modal "Demo Form", "#demo-form-" + post.id.to_s, :class=>"button" %>

And your second line:

<%=  link_to_modal "Demo Form", "#demo-form-"(post.id), :class=>"button" %>

is invalid and should have thrown a SyntaxError.

I'm unsure on your choice of syntax, but I like @Surya's answer better that uses the string interpolation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top