给定一个看起来像这样的Ruby on Rails模型(db / schema.rb):
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.string "other"
end
在我正在做的users_controller.rb
中:
def new
@user = User.new
@user.email.downcase!
@user.other = "TEST"
end
并在show
视图中show.html.haml
):
User details:
%b #{@user.name} + #{@user.email} + #{@user.other}
问题是根本不显示@user.other
值。我的猜测是@user.other
是nil
,但我不知道为什么它没有设置为TEST
,因为我在new
控制器操作中设置了它。
在我的Users
模型中,我设置了:
attr_accessible :name, :email, :other
有什么想法吗?
谢谢!
答案 0 :(得分:3)
new
和show
行为有所不同。在new
中设置内容并不意味着它会显示在show
视图中。如果您的@user.other = "TEST"
操作中有create
,那么您在视图中肯定会看到#{@user.other}
的“TEST”。