dennismonsewicz
dennismonsewicz

Reputation: 25552

Rails 3 ActiveRecord Update Attributes not working

I have trying to allow a user to edit their account (simple enough, eh?)

Here is my Update Method:

  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

Route:

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

HTML Form:

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

When the form is submitted, this is dump in the 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 '[email protected]' AND `users`.`id` != 43008) LIMIT 1
  User Exists (33.4ms)  SELECT 1 FROM `users` WHERE (`users`.`email` = BINARY '[email protected]' AND `users`.`id` != 43008) LIMIT 1
   (0.1ms)  ROLLBACK

I'm not sure why this is not working... anyone else run into this issue?

Upvotes: 0

Views: 1068

Answers (1)

Michael Durrant
Michael Durrant

Reputation: 96594

One possibility is that you have a User model and an authentication system like Devise or AuthLogic. When those are added to User you now need to explicitly declare attributes as read and write able as in

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

Upvotes: 1

Related Questions