修改Ruby on Rails控制器中的模型属性

时间:2013-07-04 07:35:48

标签: ruby-on-rails ruby-on-rails-3

给定一个看起来像这样的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.othernil,但我不知道为什么它没有设置为TEST,因为我在new控制器操作中设置了它。

在我的Users模型中,我设置了:

attr_accessible :name, :email, :other

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

newshow行为有所不同。在new中设置内容并不意味着它会显示在show视图中。如果您的@user.other = "TEST"操作中有create,那么您在视图中肯定会看到#{@user.other}的“TEST”。