Question

I created an API for my Rails application and I'm using BatmanJs for the front end. When creating a new item, Rails create it but throw me an error undefined method 'model_name' for TrueClass:Class associate to :

items_controller.rb

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

I believe it's come from my strong parameters settings. Since I'm using namespace for this API api/items, using batman, I also have to specify it in the model :

item.js.coffee

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

and then in my controller I have to change the strong parameters according to that since Batman is using resourceName as index when posting data :

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

I think my error come from the fact that when Rails try to instanciate a new item, it's tried to use the api/item as symbole and can't find an associate model. So my question is, how can I modify on the fly params.require('api/item').permit to params.require(:item).permit just before instanciate a new item so Rails knows then what to call?

Thanks

EDIT
Here is what Batman is sending to 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"}}

and here is the query it does right after (in the server log) :

(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

also the error on the server log just after the insertion :

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'
Was it helpful?

Solution 2

Ok guys I finally found where that problem came from. I needed to replace :

respond_with @item.save

by :

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

I still don't understand why it didn't work the first way as they are supposed to be the same. If someone has an explanation, I'll be more than happy to know it :)

Anyway, it's fixed now. Thanks to everyone for your help

OTHER TIPS

Can you mess with the params before calling #require ? For example:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top