代码之家  ›  专栏  ›  技术社区  ›  Martin

请求未到达以“路径”结尾的控制器操作

  •  1
  • Martin  · 技术社区  · 7 年前

    就我的一生而言,我无法理解为什么我的控制器没有进行SQL调用,然后将其传递给视图。

    在我的应用程序的每个其他部分中,包括同一控制器中的其他方法,都不用担心。但不管出于什么原因,我都做不好。

    应用程序/控制器/转介\u控制器。rb型

    class ReferralsController < ApplicationController
    
        def index # <- This works fine. 
            @referrals = Referral.all
            @referral = Referral.first 
            @reminder = Reminder.first 
            respond_to(:js, :html)
        end 
    
        def category_path # <- This doesn't work
            @referrals = Referral.where(:category => :option)
            @referral = @referrals.first 
            respond_to :js 
        end 
    
        def fetch_content # <- This works fine. 
            @referral = Referral.find(params[:follow_id])
            respond_to :js 
        end 
        ...
    end 
    

    配置/路由。rb型

    Rails.application.routes.draw do
      ...
      resources :referrals
    
      get '/referrals' => 'referrals#index'
      get '/category_path' => 'referrals#category_path', as: 'category'
      get '/fetch_content' => 'referrals#fetch_content', as: 'fetch_content'
    end 
    

    以下是我的观点中称之为这些路线的部分。

    应用程序/视图/推荐/索引。html。雇员再培训局

    <%= link_to 'Immigration Referral', category_path(:option => 'Immigration Referral'), remote: true, class: 'category-links' %> - 
    ...
    <%= link_to referral.title, fetch_content_path(:follow_id => referral.id), remote: true %>
    

    category_path fetch_content 设置几乎相同, fetch\u内容 工作正常,并且 category\u路径 甚至不进行SQL查询。我使用的位置 fetch\u内容 日志显示

    Started GET "/fetch_content?follow_id=1" for 127.0.0.1 at 2018-02-24 09:22:16 -0600
    Processing by ReferralsController#fetch_content as JS
      Parameters: {"follow_id"=>"1"}
      User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
      Referral Load (0.3ms)  SELECT  "referrals".* FROM "referrals" WHERE "referrals"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
      Rendering referrals/fetch_content.js.erb
      Rendered referrals/_content.html.erb (0.3ms)
      Rendered referrals/fetch_content.js.erb (2.6ms)
    Completed 200 OK in 19ms (Views: 14.9ms | ActiveRecord: 0.5ms)
    

    然而,当我使用 category\u路径 结果如下:

    Started GET "/category_path?option=Immigration+Referral" for 127.0.0.1 at 2018-02-24 09:22:22 -0600
    Processing by ReferralsController#category_path as JS
      Parameters: {"option"=>"Immigration Referral"}
      User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
      Rendering referrals/category_path.js.erb
      Rendered referrals/_titles.html.erb (0.7ms)
      Rendered referrals/_content.html.erb (0.4ms)
      Rendered referrals/category_path.js.erb (32.6ms)
    Completed 200 OK in 80ms (Views: 62.1ms | ActiveRecord: 0.2ms)
    

    在我看来 @referrals.nil? 正在返回 true ,如果它不进行SQL调用,我会想到这一点。那么我的问题是,为什么?!为什么我的控制器没有进行SQL调用。我甚至试着换一种更简单的方式,比如 @referrals = Referral.all 但这仍然不会触发SQL调用。发生了什么事?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sebastián Palma    7 年前

    后缀“path”似乎对控制器中的任何操作都无效,可能是因为它应该与Rails为@apreading所管理的路由冲突。

    如果使用后缀路径设置动作,并尝试列出该控制器的动作方法,则会看到该动作不存在:

    class ReferralsController < ApplicationController
      ...
    
      def category_path # <- This doesn't work
        ...
      end 
    
      def fetch_content_path # <- This works fine. 
        ...
      end 
    end
    

    控制器有两个动作完成 *_path . 现在询问您获得的行动方法:

    ReferralsController.action_methods
    => #<Set: {"new", "index", "update", "create", "edit", "destroy", "show"}>
    

    如果删除动作名称中的最后一个“h”字符,会发生什么情况

    ReferralsController.action_methods
    => #<Set: {"new", "index", "update", "create", "edit", "destroy", "show", "category_pat"}>
    

    该操作出现在那里,这就是为什么可以访问fetch\u content操作,而category one无法访问的原因。解决方法就是重命名该操作方法。