我有設置窗體。我想讓如果密碼字段爲空,更新其他屬性。 另外我正在使用設計。有沒有一些有用的方法可以幫助我呢?使用和不使用密碼更新用戶設置。導軌
form.html.erb
<%= form_for(@user, :url => url_for(:controller => 'settings', :action => 'update')) do |f| %>
<%= f.label :email, 'Email' %>
<%= f.email_field :email %>
<%= f.label :mobile, 'Mobile' %>
<%= f.phone_field :mobile %>
<%= f.label :current_password, "Current password" %>
<%= f.password_field :current_password %>
<%= f.label :password, "New Password" %>
<%= f.password_field :password, :autocomplete => "off" %>
<%= f.label :password_confirmation, "Confirm New Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Update" %>
<% end %>
user.rb
validates :password, length: {minimum: 4}, on: :update, allow_blank: true
settings_controller.rb
def update
@user = current_user
if @user.update_attributes(user_params)
sign_in(@user, :bypass => true)
flash[:success] = "Settings were successfully updated"
respond_to do |format|
format.html {redirect_to :action=> :show}
end
else
flash[:fail] = "Settings **was not updated**"
respond_to do |format|
format.html {render :action => :show }
end
end
end
有一個特殊的方法f或'update_without_password' – devanand
@devanand謝謝你的回答,但不幸的是這對我不起作用。我的表單包含密碼。如果用戶填寫此字段,我想更新它。 – MikeAndr