私は次のようなURLを持っていますhttp://example.com/posts/?tag=2
私のroutes.rbは
resources :posts do
get 'tag', :on => :collection
end
次のようなリンクが必要ですhttp://example.com/posts/tag/linux
私のテーブルは次のとおりです。
posts(id,title)
tags(id,name)
taggings(id, post_id, tag_id)
私は次のようなURLを持っていますhttp://example.com/posts/?tag=2
私のroutes.rbは
resources :posts do
get 'tag', :on => :collection
end
次のようなリンクが必要ですhttp://example.com/posts/tag/linux
私のテーブルは次のとおりです。
posts(id,title)
tags(id,name)
taggings(id, post_id, tag_id)
あなたはsthを行うことができます。お気に入り
resources :posts do
get 'tag/:name', on: :collection
end
コントローラーでは、URL パラメーターにある名前のタグを見つけて、このタグを持つすべての投稿を取得できます。
Tag.where(name: params[:name]).posts
Postまたは、モデルに関数を実装するfind_by_tag(tag)ので、呼び出すだけで済みます
Post.find_by_tag(params[:name])
コントローラーのアクションで、何が読みやすいか。