简体   繁体   中英

How to call a helper method in view

I have this code in my helper:

  def app_state(app)
    state = app.operator_apps.map(&:is_production)
    if state.include?(true) and state.include?(false)
      "Sandbox/Production"
    elsif state.include?(true)
      "Production"
    else
      "Sandbox"
    end
  end

and in my view I have done this:

<%= app.app_state %>

I get the following error:

ActionView::Template::Error (undefined method `app_state' for #):

Please help me out in solving this.

The error you are getting will persist unless an app_state method is defined somewhere within app 's model.

Try calling the helper method like so:

<%= app_state(app) %>

Now the app object is being passed in as the argument of the app_state method, instead of the method being called on the object itself.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM