我有一个控制器
class UserController < ApplicationController
def add
@post = Post.new
end
def f(title)
Something
end
end
我想从添加页面调用方法f
<%= f(Hello) %>
但是我收到了一个错误:
undefined method `f'
答案 0 :(得分:2)
将f定义为辅助方法:
class UserController < ApplicationController
def add
@post = Post.new
end
def f(title)
Something
end
helper_method :f
end
答案 1 :(得分:2)
您必须在user_helper.rb
文件上编写此方法才能在视图中访问该方法。
或,您可以使用该代码,
class MyController < ApplicationController
def my_method
# Lots of stuff
end
helper_method :my_method
end