我在我的 model/user.rb
中声明了一些简单的 boolean 字段
class User < ActiveRecord::Base
attr_accessible :name, :has_car
def init(age)
if age > 18
has_car = true
else
has_car = false
end
has_car
end
...
然后在我看来(.html.haml
文件),我尝试只打印该字段:
...
%li
- if this_user.has_car
= "This person has a car"
- else
= "This person does NOT have a car"
...
出于某种原因,this_user.has_car
的计算结果始终为 false
。
谁能告诉我我在这里做错了什么? (我是 Ruby/Rails 的新手)
谢谢
最佳答案
你可以在用户模型中定义一个名为has_car?
的方法
# user.rb
def has_car?
age > 18
end
然后在您的 View 中使用 this_user.has_car?
。
关于ruby-on-rails - ruby on rails 中的 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15149877/