0

I am using Rails 3 and am having 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?

3
  • I don't understand the question. More precisely, I don't understand how you expect the reply to look like Commented Jul 28, 2011 at 20:12
  • The point of this link is for the url to look like ".../posts/1?reply=@commenter:" This doesn't happen, and I can't figure out why. Commented Jul 28, 2011 at 20:16
  • comment.post is a valid url? and are you sure of your !viewing_post condition? Commented Jul 28, 2011 at 20:19

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I needed! I had the route setup, I just was confused on how to put the reply string in there. Thanks!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.