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

songlipeng2003 · 发布于 2012年05月02日 · 最后由 songlipeng2003 回复于 2012年05月02日 · 1977 次阅读
503
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)




共收到 19 条回复
115

哪有更新的的sql?

353
(0.1ms)  BEGIN
(0.0ms)  COMMIT

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

503

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

503

#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>

没有错吧?

279

redirect_to :root and return

503

#5楼 @jokry 什么意思?

353

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

503

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

96

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

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

503

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

503

#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


503

#7楼 @cantin

console下运行

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

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

115

试试这样

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

看看是false还是true

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

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

503

#14楼 @zhangyuan 正解啊,谢谢了

503

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

96

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

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