2013-05-18 60 views
0

我已經扔在一起快速下來和骯髒的應用程序來跟蹤一些信息的臨時基礎。我知道,不要以明文形式存儲密碼。這不是面向公衆的,它是在一個極其安全的內部盒子上;沒有人能夠獲得它,頁面不可訪問,但只是一個簡短的列表。無論如何...我試圖弄清楚爲什麼當我編輯一條記錄時,更新將一個未經編輯的記錄字段保存爲空白,當從數據庫中讀取值時,該字段爲空。在這種情況下,我從索引視圖中選擇一條記錄。我編輯該記錄,而不是觸摸密碼字段。我保存了記錄。從日誌中可以看到,該字段爲空。Rails Activerecord更新保存未編輯的字段爲空

表單代碼

<%= form_for(@swiftpwd) do |f| %> 
     <% if @swiftpwd.errors.any? %> 
      <div id="error_explanation"> 
       <h2><%= pluralize(@swiftpwd.errors.count, "error") %> prohibited this swiftpwd from being saved:</h2> 

       <ul> 
       <% @swiftpwd.errors.full_messages.each do |msg| %> 
       <li><%= msg %></li> 
       <% end %> 
       </ul> 
      </div> 
     <% end %> 

     <div class="field"> 
      Customer Name<br /> 
      <%= f.text_field :customername %> 
     </div> 
     <div class="field"> 
      Email Address<br /> 
      <%= f.text_field :emailaddr %> 
     </div> 
     <div class="field"> 
      Password<br /> 
      <%= f.password_field :password %> 
     </div> 
     <div class="field"> 
      Phone Number<br /> 
      <%= f.text_field :phone %> 
     </div> 
     <div class="field""> 
      Notes<br /> 
      <%= f.text_area(:notes, :size=>'50x10') %> 
     </div> 
     <div class="actions"> 
      <%= f.submit 'Submit' %> 
     </div> 
    <% end %> 

控制器

PUT /swiftinfo/1 
PUT /swiftinfo/1.json 
def update 
    @swiftinfo = Swiftinfo.find(params[:id]) 
    @swiftinfo.staffid = @staffid 

    respond_to do |format| 
    if @swiftinfo.update_attributes(params[:swiftinfo]) 
     format.html { redirect_to root_path, notice: 'Accounts was successfully updated.' } 
     format.json { head :no_content } 
    else 
     format.html { render action: "edit" } 
     format.json { render json: @swiftinfo.errors, status: :unprocessable_entity } 
    end 
    end 
end 

登錄

Started PUT "/swiftinfo/10" for 192.168.207.200 at 2013-05-17 16:46:57 -0700 
Processing by swiftinfoController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"tYo36rKCPtiplb+qLuF8QpcITXD/b0K869LNhakfOdg=", "swiftpwd"=>{"customername"=>"Customer name ", "emailaddr"=>"[email protected]", "password"=>"[FILTERED]", "phone"=>"", "notes"=>"Note"}, "commit"=>"Submit", "id"=>"10"} 
    Swiftinfo Load (0.1ms) SELECT "swiftinfo".* FROM "swiftinfo" WHERE "swiftinfo"."id" = ? LIMIT 1 [["id", "10"]] 
    (0.1ms) begin transaction 
    (0.3ms) UPDATE "swiftinfo" SET "password" = '', "notes" = 'Note', "updated_at" = '2013-05-17 23:46:57.586414' WHERE "swiftinfo"."id" = 10 
    (169.7ms) commit transaction 
Redirected to http://192.168.1.52:7779/ 
Completed 302 Found in 211ms (ActiveRecord: 170.2ms) 

@anonymousxxx - 快速的回答,是的,如果它被編輯和空白。但事情就是這樣,編輯時這個字段並不是空白,或者不應該是空白。我在index.html.erb屏蔽了用戶的密碼(我沒有呈現swiftinfo.password值,只是顯示「*****」。爲了測試,我顯示了密碼值(swiftinfo.password)< %= swiftinfo.password%>並且確實存在密碼,所以當我編輯該記錄(或記錄)時,密碼又有一個值,因此,如果我不編輯該字段,而是編輯另一個字段或無場,更新應該保存當前值,編輯或原來,表

更新:

好吧,我已經分離出的問題是:

<%= f.password_field :password %> - Browser displays blank, record is saved with a blank value 
%= f.text_field :password %> 

顯示密碼明文,但記錄將正確的值(當前或更改)保存到表中。

+0

如果密碼字段爲空,你不會更新密碼字段嗎? –

回答

1

如果字段爲空且不想更新字段空白,則應在控制器上更新之前刪除字段空白。試試這個:

def update 
    @swiftinfo = Swiftinfo.find(params[:id]) 
    if params[:swiftinfo][:password].blank? 
    params[:swiftinfo].delete(:password) 
    end 
    respond_to do |format| 
    if @swiftinfo.update_attributes(params[:swiftinfo]) 
     format.html { redirect_to root_path, notice: 'Accounts was successfully updated.' } 
     format.json { head :no_content } 
    else 
     format.html { render action: "edit" } 
     format.json { render json: @swiftinfo.errors, status: :unprocessable_entity } 
    end 
    end 
end