Search code examples
ruby-on-railsurlrestacts-as-taggable-on

Managing URL Parameters in Rails


Right now I am finding routing and URL constructing within rails to be semi-confusing. I have currently matched the following for tags that are passed in when displaying/filtering data.

match '/posts/standard/' => 'posts#standard'
match '/posts/standard/:tags' => 'posts#standard', :as => :post_tag
match '/posts/standard/:tags' => redirect { |params| "/posts/standard/#{params[:tags].gsub(' ', '+')}" }, :tags => /.+/

However, now I want to add a 'skill' parameter that can only take one state; however, I am very confused by how I want to construct this within my URL. I cannot simply have...

match '/posts/standard/:tags/:skill' => 'posts#standard', as => post_tag, as: => post_skill

So, I am very confused by this at this point, does Rails offer any type of help for constructing URL's?


Solution

  • One way is to just keep your main route

    match '/posts/standard/:tags' => 'posts#standard', :as => :post_tag
    

    and handle the additional URL params as params. The url would look like:

    /posts/standard/1?skill=something
    

    and it is easy enough to inject the additional params, such as by

    link_to post_tag_path(:skill=> 'something')
    

    and your controller would then do

    def standard
       if params[:skill] == 'something'
         ...
       else 
         ...
       end
    end
    

    Also, not sure about this, but your first line in your routes 'match '/posts/standard/' => 'posts#standard' may catch all of your routes since there is a match. If this is the case, simply move it to after the first line.