Question

I have a method in my posts_controller:

  def tag
    @posts = Post.all.tagged_with(params[:id]).page(params[:page]).per(10)
    render :action => 'index'
  end

My routes file :

  resources :posts do
    collection do
      get :top
      get :revelance
      get :tag
    end
  end

My view file where i output tags: <%=raw post.tag_list.map {|t| link_to t, tag_posts_path(t), class: "tags" }.join (' ') %>

The problem is: When i click on tag i get to url: */posts/tag.Sometag and the page is empty. Guess i need to change my routes file.. but I don't know what to do

Was it helpful?

Solution

You don't actually need this tag method, you could directly make the job in the index action:

def index
  @posts = if params[:tag]
    Post.all.tagged_with(params[:tag]).page(params[:page]).per(10)
  else
    Post.all.page(params[:page]).per(10)
  end
end

Then you can define your route like this:

get 'tags/:tag', to: 'posts#index', as: :tag

And adapt it in your view:

<%= raw post.tag_list.map {|t| link_to t, tag_path(t), class: "tags" }.join (' ') %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top