我为我的 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/item
as 符号并且找不到关联模型。所以我的问题是,如何在实例化新项目之前即时修改,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"}}
这是它之后(在服务器日志中)执行的查询:
(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'