Rails update_attributes 没有效果,请求帮助 已经解决

songlipeng2003 · 2012年05月02日 · 最后由 songlipeng2003 回复于 2012年05月02日 · 3428 次阅读
class AddressesController < ApplicationController
  def update
    @address = Address.find(params[:id])

    if @address.user != current_user 
      redirect_to :root
    end

    if @address.update_attributes params[:address]
      flash[:notice] = "更新地址成功"

      redirect_to :action => :edit, :id => @address
    else
      render :action => :edit, :id => @address
    end
  end
end




就这样一个简单的方法

页面显示了 更新地址成功 但是所有字段都没有更新

日志如下:

Started PUT "/addresses/1" for 127.0.0.1 at 2012-05-02 18:35:56 +0800
Processing by AddressesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"SHKjkV+LcF5xxe/ZZN/FE0CRUsC5jJNkPc0iplyacoU=", "address"=>{"province"=>"dd", "city"=>"dd", "area"=>"dd", "address"=>"dd", 
"phone"=>"wewew"}, "commit"=>"更新", "id"=>"1"}
  User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
  Address Load (0.2ms)  SELECT `addresses`.* FROM `addresses` WHERE `addresses`.`id` = 1 LIMIT 1
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 4 LIMIT 1
   (0.1ms)  BEGIN
   (0.0ms)  COMMIT
Redirected to http://0.0.0.0:3000/addresses/1/edit
Completed 302 Found in 4ms (ActiveRecord: 0.6ms)




哪有更新的的 sql?

(0.1ms)  BEGIN
(0.0ms)  COMMIT

中间没有 sql 语句,可以先查看 params[:address] 里面的参数是否正确

#1 楼 @zgm 是的啊。我知道,但是为什么呢?

#2 楼 @cantin 这是 form


<form accept-charset="UTF-8" action="/addresses/1" class="simple_form edit_address" id="edit_address_1" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="SHKjkV+LcF5xxe/ZZN/FE0CRUsC5jJNkPc0iplyacoU=" /></div>


  <div class="inputs">
    <div class="input string required"><label class="string required control-label" for="address_province"><abbr title="required">*</abbr> 省份</label><input class="string required" id="address_province" name="address[province]" size="50" type="text" value="dd" /></div>
    <div class="input string required"><label class="string required control-label" for="address_city"><abbr title="required">*</abbr></label><input class="string required" id="address_city" name="address[city]" size="50" type="text" value="dd" /></div>
    <div class="input string required"><label class="string required control-label" for="address_area"><abbr title="required">*</abbr></label><input class="string required" id="address_area" name="address[area]" size="50" type="text" value="dd" /></div>
    <div class="input string required"><label class="string required control-label" for="address_address"><abbr title="required">*</abbr> 地址</label><input class="string required" id="address_address" name="address[address]" size="50" type="text" value="dd" /></div>
    <div class="input tel required"><label class="tel required control-label" for="address_phone"><abbr title="required">*</abbr> 电话</label><input class="string tel required" id="address_phone" name="address[phone]" size="50" type="tel" value="" /></div>
  </div>

  <div class="actions">
    <input class="btn" name="commit" type="submit" value="更新" />
  </div>
</form>

没有错吧?

redirect_to :root and return

在 console 里面是可以使用 update_attributes 的吗?

#5 楼 @jokry 那几句不要也是一样

是不是因为 mass-assignment,那些属性无法被这样赋值?

是 rails 哪个版本呢,模型如何定义的?

#7 楼 @cantin 不是在 console 使用的啊,在 console 也应该可以吧

#9 楼 @zhangyuan 应该不是,没有报错啊

class Address < ActiveRecord::Base
  belongs_to :user

  validates_presence_of :province, :city, :area, :address, :phone

  attr_accessor :province, :city, :area, :address, :phone
end


#7 楼 @cantin

console 下运行

address.update_attributes :phone => 'bbb', :province=>:aa, :city=>:bb, :area=>:cc, :address=>:ee

也是一样的结果 (0.1ms) BEGIN (0.1ms) COMMIT

试试这样

@address = Address.new params[:address]
puts @address.valid?

看看是 false 还是 true

attr_accessor :province, :city, :area, :address, :phone

这个定义了 5 个属性,应该用 attr_accessible 吧?

#14 楼 @zhangyuan attr_accessor 和 attr_accessible 有什么区别啊?

#17 楼 @songlipeng2003 attr_accessor 是 ruby 里的,定义普通属性的(也就是 setter 和 getter 方法);attr_accessible是 rails 里的,用来设置可以 mass-assignment 的属性。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号