我希望通过辅助方法获得一个人的性别:
sportists_helper.rb
def sportists_gender
gender = self.gender
if gender == "1"
"Male"
elsif gender == "2"
"Female"
end
end
然后以视图:<%= sportists_gender %>
打印出来,但这会给我undefined method 'gender'
错误,为什么会这样?
答案 0 :(得分:2)
假设您有人物模型。这是一个例子:
助手:
def sportists_gender(person)
gender = person.gender
if gender == "1"
"Male"
elsif gender == "2"
"Female"
end
end
从View.erb调用:
<% @person = Person.first %> // any object according to your view.
<%= sportists_gender(@person) %> // display result.