Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Engine routes in Application Controller

I have a before_filter hook in my main app's application controller that does something like: (It doesn't just put a link in the flash, there is a message, but it isn't relevant to the question, it just accesses the route in the method)

class ApplicationController < ActionController::Base
  before_filter :set_link

  def set_link
    flash[:notice] = items_path
  end
end

This works fine for the app, however when I go into the controllers for an engine I made I get the exception

No route matches {:controller=>"items", :action=>"index"}

I understand that when in the engine, the routes helpers are for the engine unless prefixed with main_app

So changing the method in the application controller to

  def set_link
    flash[:notice] = main_app.items_path
  end

Gets rid of the exception but I really don't want to have to do that. Is there another solution to getting the engine to recognize the main_app routes?

EDIT:

This also happens if the application layout calls path helpers. So if the engine is designed to integrated into the main_app's layout then this issue will crop there up too.

like image 671
Kyle d'Oliveira Avatar asked Sep 28 '11 19:09

Kyle d'Oliveira


People also ask

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

What are routes in Rails?

Rails routes stand in the front of an application. A route interprets an incoming HTTP request and: matches a request to a controller action based on the combination of a HTTP verb and the request URL pattern. captures data in the URL to be available in params in controller actions.

What are engines in Rails?

1 What are Engines? Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the Rails::Application class inheriting a lot of its behavior from Rails::Engine .

How do Rails routes work?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.


1 Answers

You can keep the isolate_namespace, as strongly recommended by Rails Engine guide, and do this:

# inside your main_app's config/routes.rb
Rails.application.routes.draw do
  root to: 'home#index'

  mount MyEngine::Engine, at: "/" 
end
# inside your engine's controller
module MyEngine
  class SomeController << ::ApplicationController
    # include main_app's route helpers
    helper Rails.application.routes.url_helpers

  end
end

And inside your gem, make sure all the url helpers are prefixed with the correct routing proxy method (e.g. my_engine.pages_path).

Your main_app's layout and engine's controller will route and link to those url helpers correctly to the main app. You don't have to add "main_app" prefix anywhere to the main app. The only downside is you're mounting your engine's routes at main_app's root path, which could collide with any routes by the same name. This is expected anyway if you were to do non-isolate_namespace.

like image 184
konyak Avatar answered Sep 30 '22 22:09

konyak