문제

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