views:

981

answers:

3

Hi, 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
+1  A: 

your TrunksController might not be extending from the ApplicationController. The application Controller includes the Application helper so if you extend your controller form it, you should have access to those methods.

The start of your controller should be something like:

class TrunksController < ApplicationController

Pete
sorry, being a noob, I didn't quite get your response. Are you saying I already have a controller named Trunks.
mr.flow3r
+2  A: 

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 %>
ez
+2  A: 

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
Thomas Watson