【问题标题】:Confusion about routing in rails 4关于rails 4中路由的困惑
【发布时间】:2013-09-15 18:09:09
【问题描述】:

在我的 Rails 4 代码中,项目是一种资源(我为项目创建了一个数据库迁移,使用 rails generate model item column1:string ...)。我创建了一个控制器 items_controller,其中包含一个方法 htmlupdates。我使用 ajax 调用

url: '/items/htmlupdates'

当我将以下行放入 config/routes.rb 时,我的代码运行良好

get 'items/htmlupdates' => 'items#htmlupdates'

但是当我将以下内容放入 config/routes.rb 时,代码不起作用

resources :items, only: [:new, :htmlupdates, :create]

在终端日志中我收到错误消息:

Started GET "/items/htmlupdates?_12345678..." for 127.0.0.1 at 2013-09-15 10:47:54 -0700
Processing by ItemsController#show as JS
  Parameters: {"_"=>12345678...", "id"=>"grades"}

为什么它认为我正在尝试在 items 表中显示 htmlupdate 行? ajax 调用中 url 的正确格式是什么? 谢谢。

【问题讨论】:

  • 您的问题中有几个错别字。我不知道您是在此处输入错误还是由于此原因导致的错误。
  • 抱歉错别字,错误不是由于错别字造成的。
  • 那么你真的用resources items, only[:new, :htmlupdates, :create]而不是resources :items, :only => [:new, :htmlupdates, :create]吗?
  • 你的问题很难理解。您能否尝试简化并使用更好的英语。
  • 再次抱歉打错字,我使用了资源:items,仅:[:new, :htmlupdates, :create] 但它不起作用。

标签: ruby-on-rails routing ruby-on-rails-4


【解决方案1】:

Rails 资源基于七个 RESTful 操作 indexshowneweditcreateupdatedestroy,它们映射到 HTTP 动词 GET、POST、放置、删除和修补。

您不能使用resource 命令创建这些操作之外的自定义路由。对于自定义路线,请使用:

get 'items/htmlupdates', :to => 'items#htmlupdates'

有关更多信息,请查看Rails Guides 的路由文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    相关资源
    最近更新 更多