2

我为我的 Rails 应用程序创建了一个 API,并将 BatmanJs 用于前端。创建新项目时,Rails 会创建它,但会向我抛出undefined method 'model_name' for TrueClass:Class与以下相关的错误:

items_controller.rb

def create
  @item = current_user.get_items.new( item_params )
  respond_with @item.save  # Apparently come from this line
end

我相信它来自我强大的参数设置。由于我为此 API 使用命名空间api/items,使用 batman,我还必须在模型中指定它:

item.js.coffee

class App.Item extends Batman.Model
  @resourceName: 'api/items'
  @persist Batman.RailsStorage

然后在我的控制器中,我必须根据它更改强参数,因为蝙蝠侠resourceName在发布数据时用作索引:

def item_params
  params.require('api/item').permit(
    :name, :address, :postcode, :town, :phone, :email, :department_id, :country_id, :mobile, :fax, :website
  )
end

我认为我的错误来自这样一个事实,即当 Rails 尝试实例化一个新项目时,它试图使用api/itemas 符号并且找不到关联模型。所以我的问题是,如何在实例化新项目之前即时修改,params.require('api/item').permit以便params.require(:item).permitRails 知道该调用什么?

谢谢

编辑
这是蝙蝠侠发送给 Rails 的内容:

Started POST "/api/items.json" for 127.0.0.1 at 2013-11-13 11:20:29 +1100
Processing by Api::V1::ItemsController#create as JSON

Parameters: {"api/item"=>{"name"=>"Test item", "address"=>"12 street address", "fax"=>"", "phone"=>"09064521212", "mobile"=>"", "email"=>"contact@test.com", "postcode"=>"64040", "town"=>"Los Angeles", "website"=>"www.test.com", "department_id"=>"2", "country_id"=>"2"}}

这是它之后(在服务器日志中)执行的查询:

(0.5ms)  BEGIN
SQL (0.5ms)  INSERT INTO `items` (`address`, `country_id`, `created_at`, `department_id`, `email`, `fax`, `group_id`, `mobile`, `name`, `phone`, `postcode`, `town`, `updated_at`, `website`) VALUES ('12 street address', 2, '2013-11-13 00:20:29', 2, 'contact@test.com', '', 1, '', 'Test item', '09064521212', '64040', 'Los Angeles', '2013-11-13 00:20:29', 'www.test.com')
(52.7ms)  COMMIT

插入后服务器日志上的错误也是:

NoMethodError - undefined method `model_name' for TrueClass:Class:
  actionpack (4.0.0) lib/action_controller/model_naming.rb:9:in     `model_name_from_record_or_class'
  actionpack (4.0.0) lib/action_dispatch/routing/polymorphic_routes.rb:182:in `build_named_route_call'
  actionpack (4.0.0) lib/action_dispatch/routing/polymorphic_routes.rb:120:in `polymorphic_url'
  actionpack (4.0.0) lib/action_dispatch/routing/url_for.rb:159:in `url_for'
  actionpack (4.0.0) lib/action_controller/metal/rendering.rb:68:in `_process_options'
  actionpack (4.0.0) lib/action_controller/metal/streaming.rb:202:in `_process_options'
  actionpack (4.0.0) lib/action_controller/metal/renderers.rb:32:in `block in _handle_render_options'
  actionpack (4.0.0) lib/action_controller/metal/renderers.rb:30:in    `_handle_render_options'
  actionpack (4.0.0) lib/action_controller/metal/renderers.rb:26:in `render_to_body'
  actionpack (4.0.0) lib/abstract_controller/rendering.rb:97:in `render'
  actionpack (4.0.0) lib/action_controller/metal/rendering.rb:16:in `render'
  actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
4

2 回答 2

0

好吧,伙计们,我终于找到了问题的根源。我需要更换:

respond_with @item.save

经过 :

respond_to do |format|
  format.json { render json: @item.save }
end

我仍然不明白为什么它不能以第一种方式工作,因为它们应该是相同的。如果有人有解释,我会很高兴知道的:)

不管怎样,现在已经修好了。感谢大家的帮助

于 2013-11-13T22:20:52.527 回答
0

你能在打电话之前弄乱参数#require吗?例如:

def item_params
  params[:item] = params["api/item"] if params[:item].blank? && params["item/api"].present?
  item_attrs = [] # <-- your symbols here
  params.require(:item).permit(*item_attrs)
end
于 2013-11-13T20:40:43.347 回答