I'm new to Rails and building a jobboard marketplace.I'm stuck with a user signup profile form. I'm using devise(4.2) with Rails 5. I've created profile before user_create and then just redirect to profile view and now update users with registration form fields like first_name & last_name.
My signup is a 2-way process and not nested profile. step 1 - devise sign-up form with just an extra field to check role of user as user(jobseeker)or company. Step 2 - Based on this role, create separate profile forms for users and company. If profile is not committed to DB, then user also should not be saved and again user sign_up must happen.
At present, I'm trying with just one generic user form which has fields first_name and last_name. I used before_create :build_profile in user model but it just passes the Profile.create without creating it on DB. Should I overwrite 'create'or 'new' in profiles_controller or registrations_controller(devise).
The error in views/profiles/show.html.erb is: First argument in form cannot contain nil or be empty. Also on the terminal I see this for profiles: "user_id"=>"2", "id"=>"profile_id"}...does this signify something between user and profile tables in DB that is hampering the create or the routes correctly set?
routes.rb
Rails.application.routes.draw do
root "jobs#index"
devise_for :users, :controllers => {registrations: "registrations"}
resources :users do
resources :profiles, only: [:show, :update]
end
end
Registrations_controller
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(user)
if resource.class == User && current_user.role == 'user'
user_profile_path
else # to check for company user resource.class == User && current_user.role == 'company'
puts "redirecting to company profile-will do later"
end
end
end
profiles_controller.rb
class ProfilesController < ApplicationController
def show
end
def update
@profile = Profile.find_by_user_id(profile_params)
current_user.update(@profile)
end
def destroy
@profile.destroy
respond_with(@profile)
end
private
def profile_params
params.require(:user).permit(profile_attributes: [])
end
def set_profile
@profile = Profile.find(params[:id])
end
end
profile.rb
class Profile < ApplicationRecord
belongs_to :user
end
user.rb
class User < ApplicationRecord
before_create :build_profile
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
enum role: [:user, :company, :admin]
has_one :profile
def build_profile
Profile.create
true
end