对于自定义操作的放置请求,没有路由匹配[GET]

时间:2015-10-15 15:44:33

标签: ruby-on-rails ruby ruby-on-rails-4 devise routes

我有一个用户模型和控制器。我正在尝试向我的控制器发出一个put和delete请求,其中put请求绑定到用户#allow,并且删除请求被假定为指向用户#default。我在提出请求时继续收到此错误:

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

我也在为用户和管理类使用设计,因此可能与此错误有关。我不太确定。这是我的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'

这是我的用户控制器:

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

这是我的用户查看我打电话的地方,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>

我还想补充一点,当我进入视图并在浏览器中执行一个检查元素以获取put请求的链接时,我确实看到了:

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

2 个答案:

答案 0 :(得分:0)

我会运行rake routes来查看您的实际路线,因为在您的routes.rb中,您确实没有与get匹配users/:id/allow <的路线/ p>

答案 1 :(得分:0)

#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>

-

要解决您的实际问题,您需要确保已加载Rails UJS驱动程序:

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