Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i use a helper in different views

i am using refinery cms at the moment. I created an engine and with it some helpers in app/helpers/admin/. now i would like to use those helpers in my frontend view (ie. app/views/myapp/index) as well. but i can not...undefined methode error. what do i have to do short of copying the whole thing to app/helpers/? the helper looks like this

module Admin     module myHelper         def somefunc         end     end end 

so is it possible to use somefunc outside of the Admin module?

like image 351
Hans Avatar asked May 20 '11 12:05

Hans


People also ask

Can we use helper method in controller rails?

It's possible, although not very common, to use helper methods from controller actions. Before Rails 5, you had to include the helper module. In newer versions, you can use helpers in your controller with the helpers (plural) object.

How do you use the helper method in Ruby on Rails?

A Helper method is used to perform a particular repetitive task common across multiple classes. This keeps us from repeating the same piece of code in different classes again and again. And then in the view code, you call the helper method and pass it to the user as an argument.


1 Answers

The "Rails way" to include a helper from a non-standard path in a view is to use the .helper method within your controller.

class MyController < ApplicationController     helper Admin::MyHelper     ... end 

http://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper

like image 184
bobics Avatar answered Sep 21 '22 17:09

bobics