Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to rails engine route helpers outside views/controllers

Tags:

I couldn't find this anywhere and just went in circles with it so I figured someone else might benefit. How do you get access to rails url helpers in an engine?

For the core app, I can do something like this:

class Thingy < ActiveRecord::Base      include Rails.application.routes.url_helpers  ... end 

But this doesnt work in models in an engine.

like image 290
Pcushing Avatar asked Feb 04 '14 23:02

Pcushing


People also ask

How do I see routes in Rails?

TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

What are URL Helpers Rails?

Module ActionView::Helpers::UrlHelper. Provides a set of methods for making links and getting URLs that depend on the routing subsystem (see ActionDispatch::Routing). This allows you to use the same format for links in views and controllers.

What is a route helper?

A Route Driver Helper is responsible for safely and efficiently assisting in delivering, and unloading product sold to customers.

What does rake routes do?

rake routes will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.


1 Answers

Looks like you need to specify the routes that're particular to your engine. So in your engine's model, for example, you can do this:

module Blog  class Stuffy   include Blog::Engine.routes.url_helpers   ...  end end 

And now you can use the url helpers from your engine inside your engine's models.

like image 155
Pcushing Avatar answered Oct 04 '22 08:10

Pcushing