I''m trying do render a haml template that is outside of the normal rails mvc. The problem I am having is that when I instantiate view none of the _helper_methods from the controller are available to the view such as ''protect_against_forgery?, form_authenticity_token, etc''. I''ve looked through the code and can''t figure out how exactly rails includes these methods in the view. Doing the following works but it seems hackish. template = File.join("#{Rails.root}", ''lib'', ''moustache_cms'', ''templates'') context = ActionView::Base.new(template, {}, @controller, nil) _controller = @controller context.class_eval do include Rails.application.routes.url_helpers include Rails.application.routes.mounted_helpers # hackish but can''t seem to get helper_methods to working from views otherwise. _controller._helper_methods.each do |method_name| define_method(method_name) { @controller.send(method_name) } unless self.method_defined?(method_name) end end context -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/qneoH9ivqukJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Carlos Antonio da Silva
2012-Dec-04 20:36 UTC
Re: helper_methods how are they added to the view
Hey Anthony, please use the Rails Talk list or Stack Overflow to ask questions. The Rails Core list is mainly for discussions about improving and adding new features to the framework. Thanks! On Mon, Dec 3, 2012 at 9:46 PM, Anthony Kirwan <amkirwan@gmail.com> wrote:> I''m trying do render a haml template that is outside of the normal rails > mvc. The problem I am having is that when I instantiate view none of the > _helper_methods from the controller are available to the view such as > ''protect_against_forgery?, form_authenticity_token, etc''. I''ve looked > through the code and can''t figure out how exactly rails includes these > methods in the view. Doing the following works but it seems hackish. > > template = File.join("#{Rails.root}", ''lib'', ''moustache_cms'', > ''templates'') > context = ActionView::Base.new(template, {}, @controller, nil) > _controller = @controller > context.class_eval do > include Rails.application.routes.url_helpers > include Rails.application.routes.mounted_helpers > # hackish but can''t seem to get helper_methods to working from views > otherwise. > _controller._helper_methods.each do |method_name| > define_method(method_name) { @controller.send(method_name) } > unless self.method_defined?(method_name) > end > end > context > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-core/-/qneoH9ivqukJ. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. >-- At. Carlos Antonio -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.