I am using Rails 3 and am having开发者_JAVA百科 trouble inserting a query string into a link.
I have a table of posts, and each post has comments. On the posts index page I have each comment with a link to "reply" to the comment. I want to insert a query string in the link to "reply"... but it isn't working. The line I am using looks like this.
<%= link_to 'Reply', comment.post, :reply => "@"+comment.commenter+":" if !viewing_post? %>
This links to show the post and comment form just fine, but the query string never makes it into the url. Why is this happening?
it does not work because link_to is not responsible for the url. link_to only manages the tag. You need to specify the parameters in the url, you can create urls/paths with parameters by using named routes:
create a named route in your routes.rb like:
resources :posts do
resources :comments
end
then you can add parameters like:
<%=
link_to
'Reply',
post_comment_path(
comment.post,
comment,
:reply => !viewing_post? ? "@"+comment.commenter+":" : nil
)
%>
this will result in:
<a href="comments/:comment_id/posts/:post_id?reply=@whatever_commenter_is:">Reply</a>
more infos at: http://guides.rubyonrails.org/routing.html
Simon
Your comment.post
is a resource from which a path can be derived, this way you cannot pass any additional GetData - so in order to append a query string to your url you would have to do the following:
<%= link_to "whatever",some_real_path(resource,:reply => "@dude7331")%>
精彩评论