【问题标题】:Getting 404 despite having defined "resources" in config/routes.rb尽管在 config/routes.rb 中定义了“资源”,但还是得到 404
【发布时间】:2016-05-27 13:43:29
【问题描述】:

我的 config/routes.rb 中有这个

  resources :my_objects

我有 app/controllers/my_objects_controller.rb

  def edit
    respond_to do |format|
      @my_object = MyObject.find(params[:id])
      format.json {
        render :json => @my_object
      }
    end
  end

但是当我尝试使用(通过 GET)联系此 URL 时,我从 JQuery 中得到 404

http://localhost:3000/my_objects/edit/8 

我也试过

http://localhost:3000/my_objects/edit?id=8

仍然得到 404。我需要使用什么正确的 URL 来从我的编辑链接中获取数据?

【问题讨论】:

  • 你可以尝试运行rake routes并将输出粘贴到这里

标签: ruby-on-rails ruby-on-rails-4 routes http-status-code-404 config


【解决方案1】:

resources 创建的 RESTful 路由遵循以下模式:

method      path                   controller action
---------------------------------------------------------
GET        /my_objects             #index
POST       /my_objects             #create
GET        /my_objects/new         #new
GET        /my_objects/:id/edit    #edit
GET        /my_objects/:id         #show
PATCH|PUT  /my_objects/:id         #update
DELETE     /my_objects/:id         #destroy

见:Rails Routing from the Outside In

【讨论】:

    【解决方案2】:

    应该是

    http://localhost:3000/my_objects/8/edit
    

    不是

    http://localhost:3000/my_objects/edit/8 
    

    请参阅@max 答案以获得澄清。

    【讨论】:

      猜你喜欢
      • 2016-09-24
      • 2021-10-11
      • 2016-09-28
      • 2012-07-01
      • 2015-09-14
      • 1970-01-01
      • 2016-09-24
      • 2021-12-10
      • 1970-01-01
      相关资源
      最近更新 更多