Rails 4强参数自定义需要名称

时间:2013-11-12 23:54:12

标签: ruby-on-rails strong-parameters batman.js

我为我的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的命名空间,使用蝙蝠侠,我还必须在模型中指定它:

item.js.coffee

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

然后在我的控制器中我必须更改强参数,因为Batman在发布数据时使用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/item作为符号并且找不到关联模型。所以我的问题是,如何在实例化新项目之前动态修改params.require('api/item').permitparams.require(:item).permit,以便Rails知道该怎么称呼?

由于

修改
以下是蝙蝠侠发送给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'

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

好的,我终于找到了问题的来源。我需要更换:

respond_with @item.save

by:

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

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

无论如何,现在已经修好了。感谢大家的帮助