Rails控制台似乎不喜欢由分号分隔的同一行上的多个
ruby语句.每当我这样做时,下一行以?>开头.我发现只有第一个声明被执行了.你是否必须将每个声明放在一个单独的行上?
>> user = User.new user = User.new => #<User id: nil,username: "",hashed_password: "",first_name: "",last_name: "",email: "",display_name: "",user_level: 0,created_at: nil,updated_at: nil,posts_count: 0> >> user.username = "John"; hashed_password = "John"; first_name = "John"; last_name = "coltrane"; email = "John@coltrane.com"; display_name = "Johndispay"; user_level = 9; user.username = "John"; hashed_password = "John"; first_name = "John"; last_name = "coltrane"; email = "John@coltrane.com"; display_name = "Johndispay"; user_level = 9; ?> user.save user.save => true
除user.username =“John”之外的所有内容;被忽略了
解决方法
你需要说“用户”.所以Ruby知道你的意思是调用user实例的属性赋值方法.否则,您只需设置名为“hashed_password”的局部变量等.
>> user.username = "John"; user.hashed_password = "John"; user.first_name = "John"; user.last_name = "coltrane"; user.email = "John@coltrane.com"; user.display_name = "Johndispay"; user.user_level = 9;
虽然,您可以只传递要在新实例上设置的属性的哈希值,就像这样
>> user = User.new(:username => "John",:hashed_password => "John",...