Reputation: 31
Rails 3.2.12 2/27/2013
We have a full rails engine called base2_cms with a model called "Event". There is no issue on running the engine using rails server.
Next, another full rails engine called base2_cvb using base2_cms packaged as a gem.No issue in this.
Next, an application uses both of these engines as gems. We have been doing this since engines first came out, but have recently made a lot of changes. Somewhere along the line, routing stopped working. Or I guess more accurately, the rails url link helper methods.
At the application level, when using link_to with the second parameter as an instance of an event, this error occurs:
undefined method `event_path' for #<#<Class:0x007f82d52b3c20>:0x007f82d52a7240>
stack trace:
actionpack (3.2.11) lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url'
actionpack (3.2.11) lib/action_dispatch/routing/polymorphic_routes.rb:135:in `polymorphic_path'
actionpack (3.2.11) lib/action_view/helpers/url_helper.rb:111:in `url_for'
actionpack (3.2.11) lib/action_view/helpers/url_helper.rb:242:in `link_to'
base2_cms (0.9.40) app/helpers/application_helper_fieldset.rb:31:in `fieldset_combined_title'
base2_cms (0.9.40) app/helpers/application_helper_index.rb:35:in `index_start'
base2_cms (0.9.40) app/views/organizations/index.html.erb:2:in
This is not specifically polymorphic related, as we have other similar problems, such as getting this error in a different circumstance:
In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers
We have tried following this last advice in various ways without success among many other attempted solutions.
Running rake routes at the application level returns the following concerning the events model:
GET /events/:id/copy(.:format) events#copy
DELETE /events/:id/remove_organization(.:format) events#remove_organization
POST /events/:id/add_organization(.:format) events#add_organization
GET /events(.:format) events#index
POST /events(.:format) events#create
GET /events/new(.:format) events#new
GET /events/:id/edit(.:format) events#edit
GET /events/:id(.:format) events#show
PUT /events/:id(.:format) events#update
DELETE /events/:id(.:format) events#destroy
Using the rails console at the final application level, we can even do this:
1.9.2-p320 :011 > app.event_path(9)
=> "/events/9"
1.9.2-p320 :012 > app.event_path(Event.find(88))
Event Load (13.1ms) SELECT "events".* FROM "events" WHERE "events"."id" = $1 LIMIT 1 [["id", 88]]
=> "/events/88"
But, when using link_to in a view, it is unable to find the event_path method as described at the top.
Upvotes: 2
Views: 844
Reputation: 1931
When you're using engines, you have to specify if the route is of the main app or of an engine, if it's from main application you can do this with
main_app.event_path
if it's from base2_cms engine you can put
base2_cms.event_path
or if it's of base2_cvb engine:
base2_cvb.event_path
if you don't specify of where the route is it'll search the route for the current engine or from the main app if you aren't in any engine
Upvotes: 2