1

Am learning rails the way most do, by implementing a blog. I've just put tagging in and have got my article view to the point where it's displaying clickable tags when you display an article. The issue is that the links are coming out like this;

http://localhost:3000/articles?tagged_with=development

I would prefer not to have the querystring, and instead have something like;

http://localhost:3000/articles/tagged_with/development

I can't find anything relevant in the "routes inside out" guide on the rails site (lots of useful stuff in there, just not this!)

Complete code here: https://github.com/mikeyhogarth/mikeyblog

Pertinant bits are;

the link in _article.html.erb:

<%= link_to tag, articles_path(:tagged_with => tag) %>

the articles index controller:

def index

 if(params[:tagged_with])
   @tag = params[:tagged_with]
   @articles = Article.tagged_with @tag
 else
   @articles = Article.all
 end

 respond_to do |format|
   format.html # index.html.erb
   format.json { render json: @articles }
 end
end

what is the rails best practice way of doing this? Do I need to implement a "tagged_with" action and create a helper or is there some rails routing magic that can sort this out in a jiffy?

EDIT: Eventually found the answer

2 Answers 2

1

Try adding something like this to your routes (untested):

match '/articles/tagged/:tagged_with' => 'articles#index', :as => :tagged_articles

Then:

link_to(tag, tagged_articles_path(:tagged_with=>"foobar"))
Sign up to request clarification or add additional context in comments.

1 Comment

I'll give you the point anyway bud, thanks for taking the time to answer
1

I found the answer eventually: I needed a "named route". If anyone else has this question, I just put this in my routes.rb file;

match "/articles/tagged_with/:tag" => "articles#index", :as => "articles_tagged_with"

then simply relpaced my "link_to" with this;

    <%= link_to tag, articles_tagged_with_path(:tag => tag) %>, 

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.