Seal
Seal

Reputation: 1060

Devise not saving names

I've read through the other threads on this, but I still can't figure it out.

Two models, users and chefs.

When I register for chefs it does not save their first_name. Below is code.

--Application_Controller

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

 before_filter :configure_permitted_parameters, if: :devise_controller?

 protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:first_name, :email) }
  end

end

-new chef form

<%= form_for(@chef) do |f|  %>

  <%= f.label :first_name %>
  <%= f.text_field :first_name %>

  <%= f.label :last_name %>
  <%= f.text_field :last_name %>

  <%= f.label :email %>
  <%= f.text_field :email %>

  <%= f.label :password %>
  <%= f.password_field :password %>

  <%= f.label :password_confirmation, "Confirmation" %>
  <%= f.password_field :password_confirmation %>

  <%= f.file_field :photo%>
<% end %>

--chef_controller

class ChefsController < ApplicationController
  before_action :set_chef, only: [:show, :edit]

  # GET /chefs
  def index
    @chefs = Chef.all
  end

  # GET /chefs/1
  def show

  end

  # GET /chefs/new
  def new
    @chef = Chef.new
  end

  # GET /chefs/1/edit
  def edit
  end

  # POST /chefs
  def create
    @chef = Chef.new(chef_params)

    if @chef.save
      redirect_to @chef, notice: 'Chef was successfully created.'
    else
      render action: 'new'
    end
  end

  # PATCH/PUT /chefs/1
  def update
    if @chef.update(chef_params)
      redirect_to @chef, notice: 'Chef was successfully updated.'
    else
      render action: 'edit'
    end
  end

  # DELETE /chefs/1
  def destroy
    @chef.destroy
    redirect_to chefs_url, notice: 'Chef was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
     def set_chef
      @chef = Chef.find(params[:id])
     end

    # Only allow a trusted parameter "white list" through.
    def chef_params
      params[:chef]
    end
end

Upvotes: 2

Views: 1646

Answers (2)

Blueone
Blueone

Reputation: 83

Here my simple solution if you want additional fields for registration/new with devise.

In ApplicationController

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


  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :last_name, 
    :email, :phone_number, :company, :address, :city, :zipcode, ])
    #in keys you list all the input you want to accept.
  end
end

Upvotes: 0

Mandeep
Mandeep

Reputation: 9173

If you look at your question it says

When I register for chefs it does not save their first_name.

So the issue is coming when you are creating a new chef and hence in your case when a chef sign up so you need to permit extra attributes for sign up and not for sign in.

Try:

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email) }
end

Upvotes: 2

Related Questions