sethcall
sethcall

Reputation: 2907

How can I use a model from a gem in Rails 3?

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.

Upvotes: 0

Views: 111

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107728

Use scope:

scope :as => "mymodule" do
  resources :my_resources
end

Upvotes: 2

Related Questions