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.

有帮助吗?

解决方案

Try this:

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top