简体   繁体   English

没有路线与[GET]相匹配,以向自定义操作提出投放请求

[英]No route matches [GET] for put request to custom action

I have a users model and controller. 我有一个用户模型和控制器。 I am trying to make a put and delete request to my controller where the put request is tied to users#allow and the delete request is suppose to be directed to users#destroy. 我正在尝试向我的控制器发出放置和删除请求,其中放置请求绑定到users#allow,并且删除请求应该定向到users#destroy。 I However keep on getting this error when I make the request: 但是,当我发出请求时,我仍然继续收到此错误:

No route matches [GET] "/users/1/allow"

I am also using devise for users and admin classes so that might have something to do with this error. 我还在用户和管理员类中使用devise,因此可能与此错误有关。 Im not really sure. 我不太确定。 Here is my routes.rb: 这是我的routes.rb:

Rails.application.routes.draw do

  get 'users/index'
  put 'users/:id/allow', :to => 'users#allow', :as=>:users_allow
  delete 'users/:id/destroy', :to => 'users#destroy',:as => :users_destroy

  devise_for :users
  devise_for :admins

  root 'website#show'
  resources :tweets do
     put '/pass', :to => 'tweets#pass',:as => :pass
     put '/fail', :to => 'tweets#fail',:as => :fail
  end

  get '/to_json', :to=>'tweets#to_json'

end 结束

Here is my users controller: 这是我的用户控制器:

class UsersController < ApplicationController
 before_action :find_user, only:[:allow,:destroy]
   before_filter :authenticate_admin!

  def index
    @users = User.all
  end

  def allow

    find_user.update(:edit_permission=>true)
    redirect_to(:back)
  end

  def destroy
    find_user.destroy
    redirect_to(:back)
  end


  private

    def find_user
        @user = User.find(params[:user_id])
    end
end

And here is my users view where I am calling from, users/index.html.erb: 这是我从中拨打电话的用户视图users / index.html.erb:

<h1>Users#index</h1>
<p>Find me in app/views/users/index.html.erb</p>
<ul>
    <% @users.each do |user|%>


        <% if user.edit_permission == nil%>
             <li style="color:red"> <%=link_to 'approve', users_allow_path(user), method: :put  %> <%=link_to 'delete', users_destroy_path(user), method: :delete %><%= "#{user.name} #{user.email}"%> </li>
        <% else%>
            <li style="color:green"><%=link_to 'delete', users_destroy_path(user), method: :delete %><%= "#{user.name} #{user.email}"%> </li>
        <% end%>

    <%end%>
</ul>

I also want to add that when I go into the view and I do an inspect element in my browser for the link for the put request I do see this: 我还想补充一点,当我进入视图并在浏览器中为put请求的链接做一个inspect元素时,我确实看到了:

 <a rel="nofollow" data-method="put" href="/users/1/allow">approve</a>

我会运行rake routes来查看您的实际路由,因为在您的routes.rb您实际上没有与getusers/:id/allow匹配的路由

#config/routes.rb
resources :users, only: [:index, :destroy] do
   put :allow, action: :allow #-> url.com/users/:user_id/allow
end

#app/controllers/users_controller.rb
class UsersController < ApplicationController

   def index
      @users = User.all
   end

   def allow
      user.update edit_permission: true
      redirect_to :back
   end

   private

   def user
      User.find params[:user_id] #-> why return an instance var if you aren't going to use it
   end
end


#app/views/users/index.html.erb
<ul>
    <% @users.each do |user| %>
        <% options = {} %>
        <% options.allow   = ["allow", "red", "put"] %>
        <% options.destroy = ["destroy", "green", "delete"] %>

        <% option = user.edit_permission ? options.allow : [options.allow, options.destroy] %>
        <% option.each do |action,colour,method| %>
            <%= link_to action, eval("users_#{action}_path(user)"), method: method, class: colour %><%= "#{user.name} #{user.email}") %>
        <% end %>
    <% end %>
</ul>

-- -

To fix your actual problem, you need to make sure you have your Rails UJS driver loaded: 要解决实际问题,您需要确保已加载Rails UJS驱动程序:

#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require_tree .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM