@user = User.new(email: params[:user][:email],telno: params[:user][:telno],password: params[:user][:password],userid: params[:user][:userid],school: params[:user][:school],profession: params[:user][:profession]) 比如这个很长 我想美观 让代码换行怎么做
@user = User.new( email: params[:user][:email], telno: params[:user][:telno], password: params[:user][:password], userid: params[:user][:userid], school: params[:user][:school], profession: params[:user][:profession] )
当然风格也会因人而异
@user = User.new @user.email = params[:user][:email] @user.userid = ... ...
谢谢了 两位
@user = User.new(params[:user])
这个才是优美的 Ruby on Rails:
@user= User.new.assign_attributes(params[:user])
另外直接使用 params 不推荐,应该使用strong parameter
Rails 4 推荐做法:
@user = User.new(user_params) private def user_params params.require(:user).permit(:email, :telno, ........ ) end