Rails Activerecord更新将未编辑的字段保存为空白

时间:2013-05-18 00:14:14

标签: ruby-on-rails

我把一个快速下来的脏应用程序放在一起,以便在临时基础上跟踪某些信息。我知道,不要以明文形式存储密码。这不是面向公众的,它是一个极端安全的内部盒子;没有人到达它,页面不可访问,但只有一个简短的列表。无论如何......我正在试图找出为什么当我编辑一条记录时,更新将一个尚未编辑的记录字段保存为空白,当它从数据库中读取一个值时。在这种情况下,我从索引视图中选择一条记录。我编辑该记录,而不是触摸密码字段。我保存了记录。正如您在日志中看到的那样,该字段为空白。

表格代码

    <%= 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"=>"user@domain.net", "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)&lt ;%= swiftinfo.password%&gt;而且确实有一个密码。所以当我编辑那条记录(或记录)时,密码又有一个值。所以,如果我不编辑那个字段,而是编辑另一个字段或无字段,更新应将当前值(已编辑或原始值)保存到表中。

更新

好的,我已将问题与此分开:

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

显示密码明文,但记录会将正确的值(当前或已更改)保存到表格。

1 个答案:

答案 0 :(得分:2)

如果字段为空并且您不想将字段空白更新,则应在控制器上更新之前删除字段空白。试试这个:

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