I use the acts-as-taggable-on gem to add tags to posts. It is impossible to create a form to search for posts with a tag.

routes.rb:

Rails.application.routes.draw do devise_for :users, controllers: { registrations: 'users/registrations' } resources :posts resources :users, except: [:destroy, :update, :create, :new, :edit] do member do get :following, :followers end end resources :relationships, only: [:create, :destroy] get 'my_posts' => 'users#my_posts', as: 'my_posts' get 'tags/:tag_list' => 'posts#index', as: :tags root 'posts#index' end 

posts_controller.rb:

 ... def index if params[:tag_list] @posts = Post.tagged_with(params[:tag_list], parse: true, :any => true).paginate(page: params[:page]).includes(:user).order('created_at DESC') else @posts = Post.paginate(page: params[:page]).includes(:user).order('created_at DESC') end end ... 

rake routes:

  new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy user_password POST /users/password(.:format) devise/passwords#create new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel user_registration POST /users(.:format) users/registrations#create new_user_registration GET /users/sign_up(.:format) users/registrations#new edit_user_registration GET /users/edit(.:format) users/registrations#edit PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroy following_user GET /users/:id/following(.:format) users#following followers_user GET /users/:id/followers(.:format) users#followers users GET /users(.:format) users#index user GET /users/:id(.:format) users#show relationships POST /relationships(.:format) relationships#create relationship DELETE /relationships/:id(.:format) relationships#destroy my_posts GET /my_posts(.:format) users#my_posts tags GET /tags/:tag_list(.:format) posts#index root GET / posts#index 

when displaying posts, I generate tags like this:

 <% post.tag_list.each do |tag| %> <%= link_to tag, tags_path(tag) %> <% end %> 

This creates links like <a href="/tags/blabla">blabla</a> , when you click on it, the search is triggered correctly. But when I try to generate a separate form for such a search, the route mismatch error comes out.

 <%= form_tag("/tags", method: "get") do %> <%= text_field_tag(:tag_list) %> <%= submit_tag("Search") %> <% end %> 

When the button is clicked, this url is formed http://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Searchhttp://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Search /tags http://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Search utf8 http://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Search % http://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Search % http://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Search % http://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Search & http://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Search = http://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Search & http://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Search = http://127.0.0.1:3000/tags?utf8=%E2%9C%93&tag_list=taxi&commit=Search and an error

No route matches [GET] "/ tags"

if I :tag_list in routes.rb , then the search by form works, but the search by links stops.

How to generate this form?

    1 answer 1

    It turned out like this:

     <%= form_tag({controller: "posts", action: "index"}, method: "get") do %> <%= text_field_tag(:tag_list) %> <%= submit_tag("Search") %> <% end %>