尽管在config / routes.rb中定义了“资源”,但仍获得404

时间:2016-05-27 13:43:29

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

我在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

但是当我尝试使用

联系此URL(通过GET)时,我从我的JQuery获得了404
http://localhost:3000/my_objects/edit/8 

我也试过

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

仍然得到了404.从编辑链接获取数据需要使用哪个正确的URL?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

应该是

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

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

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