2013-05-18 65 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