具有嵌套关系的ActiveResource因路由错误而失败

时间:2016-06-02 19:58:18

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

我是rails世界的新手,在我的项目中,我们在服务器端有以下模型

        class Server < ActiveRecord::Base
             has_many :disks, :dependent => :destroy
            accepts_nested_attributes_for :disks, :reject_if => lambda { |a| a[:size].blank? }, :allow_destroy => true
 end

       class ServersController < ApplicationController
        def new
          @server = Server.new
          4.times { @server.disks.build }     
          respond_to do |format|
          format.xml { render :xml => @server.to_xml(:include => :disks) }
          format.json { render :json => @server.to_json(:include => :disks) }
          end
        end
     end

class Disk < ActiveRecord::Base
  belongs_to :server
  validates :size, presence: true

end

在客户端,我们有

class Server < ActiveResource::Base
  self.site = URLS_CONFIG['server_url']
  self.format = :json
  self.include_root_in_json = true 
end

class Disk < ActiveResource::Base
  self.site = URLS_CONFIG['server_url']
  self.prefix = "/servers/:server_id/"
  self.format = :json
end

当我尝试

disk1 = @server.disks.build

在客户端,它给出了错误

ActiveResource::ResourceNotFound: Failed.  Response code = 404.  Response message = Not Found.
    from /usr/local/bundle/gems/activeresource-4.0.0/lib/active_resource/connection.rb:144:in `handle_response'
    from /usr/local/bundle/gems/activeresource-4.0.0/lib/active_resource/connection.rb:123:in `request'

当我在服务器端检查时错误是

ActionController::RoutingError (No route matches [GET] "/servers/disks/new.json"):
11:21:06 web.1    |   actionpack (4.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
11:21:06 web.1    |   web-console (2.1.3) lib/web_console/middleware.rb:29:in `call'
11:21:06 web.1    |   actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
11:21:06 web.1    |   railties (4.2.2) lib/rails/rack/logger.rb:38:in `call_app'
11:21:06 web.1    |   railties (4.2.2) lib/rails/rack/logger.rb:22:in `call

谁能告诉我这里出了什么问题?我想在前端显示服务器的所有磁盘,并允许用户编辑和更新它。

0 个答案:

没有答案