0

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.
Starkers
  • 10,273
  • 21
  • 95
  • 158
  • It is the opposite way. When a new request comes to app, new instance of `ActionController::Base` is created (or any descendant) and then handles it and respond with content of your view. – blelump Oct 28 '14 at 18:05
  • @blelump Is it possible to do this for a file stored outside of the views directory? – Starkers Oct 28 '14 at 18:29
  • You need controller context or at least include appropriate helper. I think there's some solution for `Angular`, but I'm not using it and I think I can't help here. – blelump Oct 28 '14 at 18:36

1 Answers1

1

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

Community
  • 1
  • 1
Surya
  • 15,703
  • 3
  • 51
  • 74
  • I want to keep it outside of the views directory. Is it possible to do this? `root/app/assets/templates/` is the correct location for angular templates so it's not like this is some mad idea :) – Starkers Oct 28 '14 at 18:30
  • I am sorry if you get that impression. I didn't have any bad intention. You can actually use the idea given in those links I posted, however, I'll update it for more clarity, see the updated answer. – Surya Oct 28 '14 at 18:35