LightBox
LightBox

Reputation: 3425

How to solve this tagging system routing issue

I have a tag resource in routes.rb: resources :tags as well as a tag link code snippet: get 'tags/:tag', to: 'users#index', as: :tag.

The tag link code snippet allows me to click on a tag link and be routed to the user index page with a list of all users who have been tagged with that tag. The link is of the form http://localhost:3000/tags/{tag_name}

Unfortunately these two ideas are clashing because when I try to add a new tag via the tag resource, the link generated is http://localhost:3000/tags/new. Which leads to ambiguity because "new" can be either an action or a tag name.

I'm a rails beginner, any ideas on how to solve this? Most of the code I'm using is from the railscast http://railscasts.com/episodes/382-tagging?view=asciicast

Upvotes: 0

Views: 45

Answers (2)

hawk
hawk

Reputation: 5408

Try this In your UsersController

def index
  @users = User.all
  @users = @users.where(tag: params[:tag]) if params[:tag]
end

Your url

http://localhost:3000/users?tag=your_tag

Your view

link_to 'Your tag', "#{users_path}?tag=your_tag"

Upvotes: 1

BroiSatse
BroiSatse

Reputation: 44685

You could try FriendlyId gem (https://github.com/norman/friendly_id) and use show action to redirect to users controller.

Upvotes: 0

Related Questions