Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing devise routes helpers inside of rails engine views

I'm building a Rails engine called Engrave.

I have the engine mounted like so:

# Routes.rb of the host app
mount Engrave::Engine => "/engrave", :as => "engrave_engine"

Within this engine I have a controller called "PostsController". When I navigate to this controller to view a post like so: /engrave/posts/1 I get this error:

undefined local variable or method `new_user_session_path'

The PostsController in the engine is inheriting from the engine controller, which is inheriting from the application controller, like so:

module Engrave
  class PostsController < ApplicationController
  ...
end

class Engrave::ApplicationController < ApplicationController
end

The new_user_session_path is being defined by devise, which I have setup like:

devise_for :users

The call to new_user_session_path is in the layouts/application.html.erb template file in the host app

I cannot figure out why this route helper isn't available in this context. What am I doing wrong?

like image 547
Jeff Avatar asked May 23 '12 00:05

Jeff


2 Answers

I've had success doing the following in the main app's application_helper.rb:

module ApplicationHelper
  # Can search for named routes directly in the main app, omitting
  # the "main_app." prefix
  def method_missing method, *args, &block
    if main_app_url_helper?(method)
      main_app.send(method, *args)
    else
      super
    end
  end

  def respond_to?(method)
    main_app_url_helper?(method) or super
  end

 private

  def main_app_url_helper?(method)
    (method.to_s.end_with?('_path') or method.to_s.end_with?('_url')) and
      main_app.respond_to?(method)
  end
end

I've used this in mountable engines, so you do not have to sacrifice those features.

like image 153
azurewraith Avatar answered Nov 15 '22 21:11

azurewraith


Use

main_app.new_user_session_path

that should work

like image 22
StrangeDays Avatar answered Nov 15 '22 20:11

StrangeDays