tencet
tencet

Reputation: 505

Additional method for controller (rails)

I have a controller

class UserController < ApplicationController
  def add
    @post = Post.new
  end

  def f(title)
    Something

  end
end

I want call a method f from add page

<%= f(Hello) %>

But I get an error :

undefined method `f' 

Upvotes: 1

Views: 52

Answers (2)

Emu
Emu

Reputation: 5905

You have to write this method on the user_helper.rb file to access that on view.

or, You can use that code,

class MyController < ApplicationController
  def my_method
    # Lots of stuff
  end
  helper_method :my_method
end

Upvotes: 2

Sebastian vom Meer
Sebastian vom Meer

Reputation: 5251

Define f as helper method:

class UserController < ApplicationController
  def add
    @post = Post.new
  end

  def f(title)
    Something

  end
  helper_method :f
end

Upvotes: 2

Related Questions