Im新的軌道,我不使用設計。我的問題是,當我在我的表單中使用password_field作爲我的密碼,並且我只更新了subscriber_number時,密碼在我的數據庫中變爲空白。但是當我爲我的密碼使用text_field時,可以看到並保存實際值。這就是我想要做的事:Rails密碼返回無,如果空白
class EdiUser < ActiveRecord::Base
validates_presence_of :subscriber_number
validates_presence_of :password, :on => :create
end
,並在我的控制器:
def update
@trader_edi_user = TraderEdiUser.find(params[:id])
@edi_user = EdiUser.find(@trader_edi_user.edi_user_id)
#@edi_user.update_attributes(edi_user_params)
if params[:edi_user][:password].blank? == true
@edi_user.update_attributes(edi_user_subscriber_number)
else
@edi_user.update_attributes(edi_user_params)
end
redirect_to :back
end
private
def edi_user_params
params.require(:edi_user).permit(:id, :subscriber_number, :password, :password_confirmation)
end
def edi_user_subscriber_number
params.require(:edi_user).permit(:subscriber_number)
end
,這是我的表單:
<%= form_for(@edi_user, url: {controller: 'maintenance/edi_users', action: 'update'}, html: {class: "form-horizontal", id: "edit_edi"}) do |f| %>
<div class="form-group edi subs">
<label class="col-sm-3 control-label has-feedback">Subscriber Number</label>
<div class="col-sm-9">
<%= f.text_field :subscriber_number, :class => "form-control req", :required => 'true', :id => "subscriber" %>
<br>
</div>
</div>
<div class="form-group edi pass">
<label class="col-sm-3 control-label has-feedback">Password</label>
<div class="col-sm-9">
<%#= f.text_field :password, :class => "form-control", :id => "password", :minlength => 6 %>
<%= f.password_field :password, :class => "form-control", :id => "password" %>
<br>
</div>
</div>
<div class="form-group edi confirm_pass">
<label class="col-sm-3 control-label has-feedback">Confirm Password</label>
<div class="col-sm-9">
<%= f.password_field :password_confirmation, :class => "form-control", :id => "confirm_password", data: { :match => "#password"} %>
<br>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="pull-right">
<%= button_tag type: "submit", class: "btn btn-md btn-success raised pull-right", id: "update_btn2" do %>
<i class="fa fa-check" aria-hidden="true"></i> <%= t('Button.Update') %>
<% end %>
</div>
</div>
</div>
<% end %>
是我對我的更新動作的代碼上我的控制器錯了?或者有什麼我失蹤?
非常感謝你提前
剛確定.....請告訴我你不是以純文本存儲密碼?! –
nope。我正在使用attr_encrypted來存儲我的密碼 –
'user_name'不在您的表單或代碼中,您如何更新它? –