0

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

这是我的更新方法:

  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

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

4

1 回答 1

1

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

# User model (models/user.rb)
attr_accessor :email, last_name, first_name # etc.
于 2012-08-14T01:42:57.043 回答