首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以请求未到达的“路径”结尾的控制器操作

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

Stack Overflow用户
提问于 2018-02-24 15:46:40
回答 1查看 74关注 0票数 1

对于我的生活,我不明白为什么我的控制器不进行SQL调用,然后将它传递到视图中。

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

app/controllers/referrals_controller.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<%= 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的地方日志显示

代码语言:javascript
复制
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时,结果是:

代码语言:javascript
复制
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调用,我就会期望它返回true。我的问题是,为什么?!为什么我的控制器不进行SQL调用。我甚至尝试更改为更简单的东西,如@referrals = Referral.all,但这仍然不会触发SQL调用。发生什么事了呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-24 17:19:23

后缀"path“似乎不适用于控制器中的任何操作,可能是因为它应该与Rails管理的路由发生冲突,如@apneadiving所指出的那样。

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

代码语言:javascript
复制
class ReferralsController < ApplicationController
  ...

  def category_path # <- This doesn't work
    ...
  end 

  def fetch_content_path # <- This works fine. 
    ...
  end 
end

控制器有两个动作,用*_path完成。现在,请使用您获得的操作方法:

代码语言:javascript
复制
ReferralsController.action_methods
=> #<Set: {"new", "index", "update", "create", "edit", "destroy", "show"}>

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

代码语言:javascript
复制
ReferralsController.action_methods
=> #<Set: {"new", "index", "update", "create", "edit", "destroy", "show", "category_pat"}>

操作出现在那里,这就是为什么可以到达您的fetch_content操作,而类别之一却不能。解决方案只是重命名操作方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48964595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档