【问题标题】:Controller action ending in "path" not being reached by request请求未达到以“路径”结尾的控制器操作
【发布时间】:2018-02-24 15:46:40
【问题描述】:

对于我的一生,我无法弄清楚为什么我的控制器没有进行 SQL 调用然后将其传递给视图。

在我的应用程序的所有其他部分,包括同一控制器中的其他方法,不用担心。但无论出于何种原因,我都无法做到这一点。

app/controllers/referrals_controller.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 

Config/routes.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 

以下是我的观点中调用这些路线的部分。

app/views/referrals/index.html.erb

<%= 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_pathfetch_content 的设置几乎相同,但fetch_content 可以完美运行,而category_path 甚至不进行SQL 查询。我在哪里使用fetch_content 日志显示

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_path 时,结果是这样的:

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 调用。发生了什么?

【问题讨论】:

  • 要访问正在发送的选项,您必须从参数中进行,尝试Referral.where(category: params[:option]),您会得到什么?
  • 好吧,正如我在帖子末尾所说的那样,我尝试做一些简单的事情,比如referral = Referral.all,但没有任何反应。
  • 我会避免命名以 path 结尾的动作,因为它与 rails 路由冲突
  • 只需将您的操作 category_path 重命名为其他任何内容,Rails 应该会给您一个警告或错误,因为这种操作方法不存在。

标签: ruby-on-rails activerecord controller ruby-on-rails-5.1


【解决方案1】:

似乎后缀“路径”对于控制器中的任何操作都无效,可能是因为它应该与 Rails 管理的路由发生冲突,正如 @apneadiving 所指出的那样。

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

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_content 操作可以到达而类别一不能到达的原因。解决方法就是重命名action方法。

【讨论】:

  • 是的!这正是问题所在。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 2015-09-10
相关资源
最近更新 更多