ruby-on-rails – 在Rails控制台中以分号作为语句分隔符

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在Rails控制台中以分号作为语句分隔符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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_pa​​ssword”的局部变量等.
>> 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",...
原文链接:https://www.f2er.com/ruby/267469.html

猜你在找的Ruby相关文章