所以我有一個用戶模型,包括登錄名,電子郵件地址,密碼,密碼確認,名稱,頭像(圖片)等。前5個驗證,基本上說明所有5個都需要存在才能創建一個新模型。Rails - update_attributes來驗證
但是,這對我來說更新有關問題。
我有一個編輯頁面,用戶只能編輯他們的名字和頭像。我目前不打算讓他們更改他們的登錄名,我希望從其他頁面更改電子郵件和密碼。
所以編輯形式如下:
<% form_for @user, :html => { :multipart => true } do |u| %>
<p>
<label>Name:</label>
<%= u.text_field :name %>
</p>
<p>
<label>Avatar:</label>
<%= display_user_avatar %>
<%= u.file_field :avatar%>
</p>
<p>
<%= submit_tag %>
</p>
<% end %>
如果我試圖做一個@user.update_attributes(params[:user])
,則因爲只有2個PARAMS是name
和avatar
,更新失敗,因爲東西像密碼,確認密碼,電子郵件等是必需的,以驗證條目,他們根本不存在這種形式。
我可以通過做@user.update_attribute(:name, params[:user][:name])
來解決這個問題,但是我擔心避免驗證是否是一件好事?尤其是關於密碼更新等問題,我需要驗證新密碼。
還有別的辦法嗎?
如果我是做到這一點簡單地使用update_attribute爲:name
和:avatar
,我怎麼會去這樣做呢?
會這樣嗎?
params[:user].each do |attribute|
@user.update_attribute(attribute, params[:user][attribute])
end
這是一個可接受的方式來做到這一點...?
--edit作爲跟進 -
Okie,我想如你所說,做
所以它做的!
版本,異常夾在瀏覽器中顯示&是:
Validation failed: Password is too short (minimum is 5 characters)
服務器日誌中的信息是:
Processing UsersController#update (for 127.0.0.1 at 2010-07-18 11:56:59) [PUT]
Parameters: {"user"=>{"name"=>"testeeeeee"}, "commit"=>"Save changes", "action"=>"update", "_method"=>"put", "authenticity_token"=>"BMEGRW/pmIJVs1zlVH2TtZX2TQW8soeCXmMx4kquzMA=", "id"=>"tester", "controller"=>"users"}
Urm。看着這個,我剛剛意識到它正在提交"id"=>"tester"
。現在,我設置了路由,以便顯示用戶登錄名,而不是user_id ...可能是爲什麼?它試圖找到user_id == tester
用戶的更新,但由於它不存在,它會嘗試創建一個? 實際上是由於路線我做錯了嗎?
嗯......耙路線告訴我的路線是:
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
我設置了這樣的路線在user.rb
文件:
def to_param
"#{login}"
end
,但它肯定被顯示的login
代替id
這麼長的時間。但是在更新操作開始時我也正確地做了一個@user = User.find_by_login(params[:id])
,然後更新@user
。
我很困惑。 >。 <
二更新:
我User.rb
驗證的東西如下:
validates_length_of :login, :within => 3..20
validates_length_of :password, :within => 5..20
validates_presence_of :login, :email, :password, :password_confirmation, :salt, :name, :on => :create
validates_uniqueness_of :login, :case_sensitive => false
validates_confirmation_of :password
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "format is invalid."
attr_accessor :password, :password_confirmation
而且hashed_password部分是在這裏:
def password=(pass)
@password = pass
self.salt = User.random_string(10) if !self.salt?
self.hashed_password = User.encrypt(@password, self.salt)
end
u.attributes
給我
>> u.attributes
=> {"salt"=>"NHpH5glxsU", "name"=>"test er", "avatar_updated_at"=>nil, "updated_at"=>Sat Jul 17 07:04:24 UTC 2010, "avatar_file_size"=>nil, "avatar_file_name"=>nil, "hashed_password"=>"84f8675c1ed43ef7f8645a375ea9f867c9a25c83", "id"=>1, "avatar_content_type"=>nil, "login"=>"tester", "email"=>"[email protected]", "created_at"=>Fri May 07 10:09:37 UTC 2010}
Urmmm ......好了,這是你說的,關於虛擬屬性password
是不存在的實際... 那麼,如何避開那? Bugger,在這裏我認爲我正在用我自己的驗證代碼擺弄...
更改爲其中一個身份驗證插件有多簡單?我需要創建一個新的用戶模型嗎?還是應該插件能夠使用我現有的插件?
感謝所有幫助到目前爲止,btw! :D
PS:是的,'
你描述的內容對我來說聽起來不太合適,我敢肯定,如果你使用'Hash'做'update_attributes'只包含一部分屬性,那麼所有其他的屬性都保留下來他們之前的值意味着驗證不應該失敗 – mikej 2010-07-17 15:53:48