dimanche 26 juillet 2015

Rails create profile after sign up in devise

i have installed devise in my app and now i want to create profile just after the individual(user) signed up and redirect them to the profile page

here is my individual model

class Individual < ActiveRecord::Base
  has_one :profile
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

my profile model is

class Profile < ActiveRecord::Base
  belongs_to :individual
  before_create :build_profile
end

Migration file for profile is

class CreateProfiles < ActiveRecord::Migration
  def change
    create_table :profiles do |t|
      t.belongs_to :individual, index: true
      t.string :first_name
      t.string :last_name
      t.string :age
      t.string :birth_date
      t.string :gender
      t.string :bio
      t.string :linkedin_profile
      t.string :facebook_profile
      t.string :twitter_profile
      t.integer :mobile_no

      t.timestamps null: false
    end
  end
end

and my profiles controller is given as

class ProfilesController < ApplicationController
    before_action :authenticate_individual!
    before_action :find_profile, only: [:show, :edit, :update, :destroy]

    respond_to :html

    def index
      @profiles = Profile.all
    end

    def new
      @profile = current_individual.build_profile
    end

    def create
      @profile = current_individual.build_profile(profile_params)
      if @profile.save
        flash[:success] = "Profile saved"
        redirect_to current_individual_path
      else
        flash[:error] = "Error"
        render :new
      end
    end

    def show
      @profile = Profile.find(params[:id])
    end

    def edit
    end

    def update
      @profile.update(profile_params)
      respond_with(@profile)
    end


    private

    def find_profile
      @profile = Profile.find(params[:id])
    end

    def profile_params
      params.require(:profile).permit(:first_name, :last_name, :birth_date,
                                      :gender, :bio, :personal_website, :linkedin_profile, :facebook_profile,
                                      :mobile_no, :telephone_no)
    end
  end

and i have the routes as

devise_for :individuals

please tell me how can the user will be signed up and after signing in they would be redirected to profile's edit view where the individual can edit the profile Thank you !!

Aucun commentaire:

Enregistrer un commentaire