0

I have a gem that has some ActiveRecord-derived models. They are tested and work.

I've added a dependency to that gem, but when I try to access pages that refer to that model in a form_for statement, for example, I get the dreaded method_missing error:

undefined methodmygem_mymodel_path'`

Note I had the models in my app/models directory as is usual and all was well; migrating my models to this gem has been the cause for grief.

UPDATE 1: In response to Robin's question:

> rails console
> MyModel
=> MyGem::MyModel(id:string, name:string)

Update 2: For Robin's request for form code Form erb:

<%= form_for(MyModel.new, :remote => true, :html => { :class => "new_mymodel_form", :id => "new_mymodel_form"}) do |f| -%>

After Robin's suggestions, this is the only way I've found around it:

UPDATE 3: a ugly workaround

With a model called Mymodel and a module called MyModule:

post '/mymodels', to: 'mymodels#create', as: 'my_module_mymodels

Because 'as' let's you control the path symbol name. I'd much rather use 'resources' macro, but it seems it doesn't know to add the module name to the path symbol when it creates it, even though form_for appends the module name. I assume there is a way to solve this, but I can't find anything about this scenario.

sethcall
  • 2,837
  • 1
  • 19
  • 22
  • I think I've been bitten by this: http://stackoverflow.com/questions/4074830/adding-lib-to-config-autoload-paths-in-rails-3-does-not-autoload-my-module. Am about to find out. – sethcall Jul 09 '12 at 21:40
  • 1
    Your error does not mean that the model can't be found. It's about the routes. To make sure your app is loading your model, run the console `rails s` and type the name of your model. – Robin Jul 09 '12 at 21:54
  • That's a good pointer, thank you. I'm looking in that direction now. I verified that the model is known by Rails using the console, thanks. – sethcall Jul 09 '12 at 21:59
  • I'm using resource :model in my routes. I can only guess that this 'macro' is unable to do the right thing. I've tried include MyModule in that file (as well as elsewhere 'earlier' in the application load process), but I'm wondering if the problem isn't that I'm using a symbol from a different module. I suppose I can just write the 7 paths 'resource' creates... – sethcall Jul 09 '12 at 22:04

1 Answers1

2

Use scope:

scope :as => "mymodule" do
  resources :my_resources
end
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261