开发者

Ruby on Rails query string not working

开发者 https://www.devze.com 2023-03-23 11:47 出处:网络
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 l

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")%>

0

精彩评论

暂无评论...
验证码 换一张
取 消