Ruby on rails Rails 4强参数自定义要求名称

Ruby on rails Rails 4强参数自定义要求名称,ruby-on-rails,strong-parameters,batman.js,Ruby On Rails,Strong Parameters,Batman.js,我为我的Rails应用程序创建了一个API,并将BatmanJs用于前端。创建新项时,Rails会创建它,但会为TrueClass:Class抛出一个错误未定义的方法“model\u name”关联到: 项目\u controller.rb def create @item = current_user.get_items.new( item_params ) respond_with @item.save # Apparently come from this line end 我

我为我的Rails应用程序创建了一个API,并将BatmanJs用于前端。创建新项时,Rails会创建它,但会为TrueClass:Class抛出一个错误
未定义的方法“model\u name”
关联到:

项目\u controller.rb

def create
  @item = current_user.get_items.new( item_params )
  respond_with @item.save  # Apparently come from this line
end
我相信这是因为我的强大参数设置。由于我正在使用batman为这个API
API/items
使用名称空间,因此我还必须在模型中指定它:

item.js.咖啡

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/item
作为符号,并且找不到关联模型。因此,我的问题是,如何在实例化一个新项目之前动态修改
params.require('api/item').permit
params.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"}}
下面是它紧接着执行的查询(在服务器日志中):

另外,插入后服务器日志上的错误:

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'

在调用
#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
def项目参数
参数[:项]=参数[“api/item”]如果参数[:项]。空白?&&参数[“项目/api”]。是否存在?

item_attrs=[]。好了,伙计们,我终于找到了问题的根源。我需要替换:

respond_with @item.save
作者:

我仍然不明白为什么第一种方法不起作用,因为它们应该是一样的。如果有人有解释,我会非常高兴知道:)


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

我对蝙蝠侠不熟悉,你能说出蝙蝠侠的实际要求吗。因此,这将是一个更清楚的工作。谢谢你的回答。我更新了我的帖子。在我看来,您可能需要为此案例编写一个测试,看看它是如何运行的。这可能是您的问题:
require('api/item')
->参数是什么样子的?如果没有一个特定的名称(比如项目),可能会影响Rails的后端。您好,谢谢您的回答。是的,这就是我在帖子中描述的问题,所以我在寻找是否有可能在从前端获取params后,但在调用save之前,动态地更改它。想法?很好,谢谢你的回答,但它不起作用。顺便说一句,这是个不错的主意,但我想问题可能来自其他地方:(
respond_with @item.save
respond_to do |format|
  format.json { render json: @item.save }
end