SoSimple
SoSimple

Reputation: 701

Rails 4/Devise: Attribute not saving

I'm using Devise with rails 4 for user authentication. I've customized the registrations/edit form before and added fields as needed with no trouble. With my latest modification, I added two more fields(category and website) to the table. They show up in db/schema and they're getting passed when I save the form but the values are not getting saved.

My form:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>

....preceding fields....

      <div class="form-field-area">
          <p class="form-label"><%= f.label :category_name %></p>
          <p class="form-label"><%= f.collection_select :category_name, Category.order(:name), :id, :name, :include_blank => true %></p>
      </div>

      <div class="form-field-area">
          <p class="form-label"><%= f.label :website %></p>
          <p class="form-label"><%= f.text_field :website, :value => current_user.website %></p>
      </div> 

<% end %>

I do have a Users controller as well, with an update method to allow me to assign roles using rolify from the website. I can't remember if my trouble with saving attributes to the devise table only started since then but here's the User controller anyway:

    def edit
        @user = User.find(params[:id])  
    end 

  def update
    @user = User.find(params[:id])
    @user.add_role params[:user][:role]

    params[:user].delete(:password) if params[:user][:password].blank?
    params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
    if @user.update(user_params)
        redirect_to user_index_path, notice: "Update Successful"
    else
      render :edit
    end
  end

And my part of my routes, just in case:

devise_for :users, controllers: {:registrations => "registrations"}

resources :users do
  resources :posts
  resources :adverts
end

Like I said, the issue I'm having is that the category and website fields on my devise form are not saving and I don't know if it's because I've added this custom update method to a Users controller or not. No error is being thrown.

EDIT

Here's a print out of the entire action from the logs

Started PUT "/users" for 192.168.0.13 at 2014-09-09 16:38:51 +0100
Processing by RegistrationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"sJHiKHOtWBQkQHIWopu1gk4ZDyW/WaknbcAya3cN8iM=", "user"=>{"full_name"=>"Sheeka Patak", "email"=>"[email protected]", "password"=>"[FILTE
RED]", "password_confirmation"=>"[FILTERED]", "business_name"=>"Fake Company", "trading_name"=>"", "category_name"=>"7", "phone"=>"01 666-7777", "website"=>"www.fakeaddress.com", "street_lin
e_one"=>"", "street_line_two"=>"", "town"=>"", "about"=>"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. N
ulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent tacit
i sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor.", "opening_times"=>"Mon - Fri: 9 -
 5\r\nSat        : 10 - 3\r\nSun       : Closed", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}
  User Load (5.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 47  ORDER BY "users"."id" ASC LIMIT 1
  User Load (4.7ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 47]]
Unpermitted parameters: trading_name, category_name, website
   (0.2ms)  begin transaction
   (0.2ms)  commit transaction
Redirected to http://192.168.0.20:3000/
Completed 302 Found in 830ms (ActiveRecord: 10.8ms)

Upvotes: 1

Views: 1086

Answers (1)

Mandeep
Mandeep

Reputation: 9173

If you look at your logs, it says:

Unpermitted parameters: trading_name, category_name, website

You need to permit your attributes. If you look at devise docs, for updating your account you need to use :account_update in your devise_paramter_sanitizer. Add this in your application_controller.rb

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:full_name, :email, :password, :business_name, :trading_name, :category_name, :phone, :website, :street_line_one, :street_line_two, :town, :about, :opening_times,   ) }
  end
end

Upvotes: 2

Related Questions