我是新来的rails,并有一个简单的问题。在Rails中部分更新模型3
我有一个滑轨模型:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation, :description
email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, :presence => true
validates :name, :presence => true
validates :password, :presence => true
...
end
在我的页面更新这个模型我有一个包含电子邮件的文本框和名称的文本框的形式。
在我的控制器我有以下更新方法:
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(:name => params[:name], :email => params[:email])
flash[:success] = "Profile updated"
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
@title = "Edit user"
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
这是一个奇怪的错误信息失败:
undefined method `downcase' for nil:NilClass
可有人告诉我,我错了吗?我知道我在做什么愚蠢的,但不能制定出它是什么......
道歉...这是一个错字。我确实有一个哈希 - 我已经更新了这个问题。我也使用form_for帮手。问题是我没有在窗体上的密码属性。如果我尝试@ user.update_attributes(params [:user]),它会失败,因为需要密码。 – Paul 2010-10-14 18:12:19