Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a method in application helper from a view?

I defined a custom method in application_helper.rb file like the following:

def rxtrnk(line)     rxTRNK = /\w{9,12}/m     trnks = Array.new     i = 0     while i <= line.size         if line[i].match(rxTRNK)             trnks[i] = line[i].scan(rxTRNK)         end         i += 1     end      return trnks   end 

Then I tried to call it from a view like so:

<% @yo = rxtrnk(@rts)%> 

But I get an error page like this:

NoMethodError in TrunksController#routesperswitch  undefined method `rxtrnk' for #<TrunksController:0x7f2dcf88> 

I know this is a very newbie question, but I couldn't find solution from googling :( Thanks for your help

edit/ here is the full application_helper.rb

module ApplicationHelper      def rxtrnk(line)     rxTRNK = /\w{9,12}/m     trnks = Array.new     i = 0     while i <= line.size         if line[i].match(rxTRNK)             trnks[i] = line[i].scan(rxTRNK)         end         i += 1     end      return trnks   end  end 
like image 960
Adam Lee Avatar asked Aug 12 '09 14:08

Adam Lee


People also ask

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.

What are helper methods in Ruby?

A helper is a method that is (mostly) used in your Rails views to share reusable code. Rails comes with a set of built-in helper methods. One of these built-in helpers is time_ago_in_words . This method is helpful whenever you want to display time in this specific format.

What is Application Helper?

A helper application is any application that handles files received by a Web browser. It is non-native and unviewable by the browser. A browser invokes a helper application through a prebuilt component in the browser's stored list of applications.


2 Answers

not sure what is the issue, but you can solve this by include the application_helper in the controller

class TrunksController      include ApplicationHelper end 

In the view call:

<%= @controller.rxtrnk %> 
like image 132
ez. Avatar answered Oct 11 '22 14:10

ez.


You should make sure that the helper containing the method you want to call is included by the current controller (in your case you want to include the ApplicationHelper). This is controlled using the "helper" method in top of controllers.

Many Rails developers just include all helpers by default to avoid having to think about this. To do this add "helper :all" to the top of your ApplicationController:

class ApplicationController < ActionController::Base   helper :all end

You can also choose to only include the ApplicationHelper:

class ApplicationController < ActionController::Base   helper ApplicationHelper end
like image 38
Thomas Watson Avatar answered Oct 11 '22 16:10

Thomas Watson