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
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.
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.
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.
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 %>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With