I do a little exercise with the device. I am trying to make a button to remove the user checkbox on ID. The error gets out:

ActiveRecord :: RecordNotFound in UsersController # show
Couldn't find User with 'id' = edit_multiple

# routes.rb root to: 'pages#home' devise_for :users get '/users', to: 'users#users' get 'users/:id', to: 'users#show' post 'users/:id', to: 'users#edit_multiple', :as => :admin_edit_user resources :users do collection do post :edit_multiple end end # кнопка в users.html.rb <%= link_to "Delete", edit_multiple_users_path(:id) %> # users_controller.rb def users @users = User.all end def show @user = User.find(params[:id]) if @user.nil? redirect_to users_path end end def destroy @user = User.find (params[:id]) @user.destroy if @user.destroy redirect_to root_url, notice: "User has been deleted" end end def edit_multiple if params[:commit] == "Delete" User.where(id: params[:user_ids]).destroy_all elsif params[:commit] == "Lock" User.where(id: params[:user_ids]).each do |user_to_lock| user_to_lock.lock_access! end elsif params[:commit] == "Unlock" User.where(id: params[:user_ids]).each do |user_to_unlock| user_to_unlock.unlock_access! end end redirect_to root_url end 

    2 answers 2

    Always read the errors carefully. There is actually useful information. Let's look at your example.

    First, this entry:

    ActiveRecord :: RecordNotFound in UsersController # show Couldn't find

    tells us that we hit the UsersController#show . That is, in the action show .

    Second, the value of params[:id] is edit_multiple

    User with 'id' = edit_multiple

    How did that happen? Let's try to figure it out.

    You have an error in the call that forms the link_to helper, as noted by @Ilya Konyukhov. Instead of a POST request, the server receives a GET request.

    Note that routing in the rails works on the principle of "first coincidence of the route" and then ceases to search. Therefore, on our GET request, the route will be chosen along the path /users/edit_multiple.id

    get 'users/:id', to: 'users#show'

    and accordingly instead of :id(.:format) will be edit_multiple.id . that is, in the params[:id] controller, edit_multiple will be equal and params[:format] will be equal to id

    Now try to fix it. In order for the link_to form a POST request, you need to specify this in the helper:

     <%= link_to "Delete", edit_multiple_users_path(:id), method: :post %> 

    Hurray we get into the edit_multiple action. And params[:id] still edit_multiple When searching for a suitable route, the router safely ignored

    get 'users/:id', to: 'users#show'

    but found the first suitable route for the method of our POST request:

    post 'users/:id', to: 'users#edit_multiple', :as => :admin_edit_user

    and according to this route, the pattern paths /users/:id(.:format). again we get params[:id] equal to edit_multiple , and params[:format] is equal to id , and params[:user_ids] completely absent.

    and the route we need:

     resources :users do collection do post :edit_multiple end end 

    ignored.

    In order for us to form the correct query in the view, call the link_to call as follows

     <%= link_to "Delete", edit_multiple_users_path(user_ids: @user_ids, commit: 'Delete'), method: :post %> 

    where @user_ids stores what you want to transfer to params [: user_ids]. Your way to call:

     edit_multiple_users_path(:id) 

    does not define at all :user_ids , but determines :format . Remember the route in the / /users/edit_multiple(.:format) .: /users/edit_multiple(.:format) route.

    Finally, the routing config:

     root to: 'users#index' devise_for :users get '/users', to: 'users#users' get 'users/:id', to: 'users#show' resources :users do collection do post 'edit_multiple' end end 

    References:

    1. about link_to here

    2. Routing at RoR

    3. Devise

    • @jussbased, fixed a couple of small errors in his answer. - Yauhen
    • fine, the error is gone. but the delete button simply redirects to root. you do not know what could be wrong? - jussbased
    • Not just redirects: in the edit_multiple action edit_multiple after working off, redirect to root_url - redirect_to root_url - Yauhen takes place

    The link_to link_to "Delete", edit_multiple_users_path(:id) creates a link that clicks on a GET request.

    View the application routing table with the rake routes command