Starkers
Starkers

Reputation: 10541

Include ActionController::Base outside of views directory

I can have a view file that contains only this:

root/app/views/layouts/application.html.erb

<%= link_to root_url %>

And of course it works. This is because

ActionController

is included in the view file somehow.

How does this work? Inside each view we don't write 'include ActionController' so how is it magically included?

Let's say I'm using an angular template:

root/app/assets/templates/angularview.html.erb

<%= link_to root_url %>

Everything works perfectly apart from the fact that the link_to isn't included in this view:

undefined method `link_to' for #<#<Class:0x000000020417b0>:0x0000000468f2c8>
  1. How should I include ActionController into a file stored at root/app/assets/templates/angularview.html.erb?
  2. What should I edit in my project to automatically make all files inside root/app/assets/templates include ActionController? Is this possible? I want them to behave like 'normal' views, and magically include everything a normal view includes.

Upvotes: 0

Views: 762

Answers (1)

Surya
Surya

Reputation: 16002

It is not working as you have your angularview template in assets directory(as you have mentioned in your question: root/app/assets/templates/angularview.html.erb). You need to create it inside your application's app/views/layouts/ directory.

See these answers for more information:

  1. https://stackoverflow.com/a/6951986/645886
  2. https://stackoverflow.com/a/19849989/645886

UPDATE: However, if you must do that then you can create an initializer and put this code:

Rails.application.assets.context_class.class_eval do
  include ActionView::Helpers
  include MyAppHelper
  include Rails.application.routes.url_helpers
end

Source: https://stackoverflow.com/a/14284279/645886

Upvotes: 1

Related Questions