Rails 3 ActiveRecord更新属性不起作用

时间:2012-08-14 01:28:32

标签: ruby-on-rails-3 activerecord attributes

我试图允许用户编辑他们的帐户(简单,嗯?)

这是我的更新方法:

  def update
    @user = current_user
    # @user.attributes = {'mail_chimp_newsletters' => []}.merge(params[:user])

    if @user.update_attributes(params[:user])
      sign_in @user, :bypass => true
      redirect_to edit_account_path, :notice => 'Account has been updated!'
    else
      redirect_to edit_account_path, :flash => { :alert => "Account could not be updated. Please try again" }
    end
  end

路线:

match 'account/edit' => 'settings#edit', :as => 'edit_account', :via => :get
match 'accounts' => 'settings#update', :as => 'accounts', :via => :put

HTML表单:

<%= form_for @user, :url => accounts_url, :method => :put, :html => { :id => "personal-info-form", :class => "hide" } do |f| %>

提交表单时,这是console

中的转储
User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 43008 LIMIT 1
   (0.2ms)  BEGIN
  User Exists (36.1ms)  SELECT 1 FROM `users` WHERE (`users`.`username` = BINARY 'test@test.com' AND `users`.`id` != 43008) LIMIT 1
  User Exists (33.4ms)  SELECT 1 FROM `users` WHERE (`users`.`email` = BINARY 'test@test.com' AND `users`.`id` != 43008) LIMIT 1
   (0.1ms)  ROLLBACK

我不确定为什么这不起作用......还有其他人遇到过这个问题吗?

1 个答案:

答案 0 :(得分:1)

一种可能性是您拥有用户模型和身份验证系统,如Devise或AuthLogic。将这些属性添加到用户后,您现在需要将属性显式声明为可读取和写入,如

# User model (models/user.rb)
attr_accessor :email, last_name, first_name # etc.