Create another model when registering a Devise user
I am trying to create a profile model for my users registering for a site. Since I have it now, the profile model is created on registration with the correct foreign key. My problem is trying to update the profile model after the user has gone through the confirmation steps.
My users are called "artists".
### /artists/registrations_controller.rb ###
class Artists::RegistrationsController < Devise::RegistrationsController
# GET /resource/sign_up
def new
super
@profile = @artist.build_artist_profile
end
# POST /resource
def create
super
@profile = @artist.create_artist_profile(profile_params)
end
private
def profile_params
params.permit(:biography, :location, :genre, :members, :facebook_url, :twitter_url, :youtube_url, :itunes_url, :amazon_url)
end
end
### /artists/profiles_controller ###
class Artists::ProfilesController < ApplicationController
before_action :authenticate_artist!
before_action :correct_artist
before_action :set_artist
def edit
@profile = ArtistProfile.find_by(params[:artist_id])
end
def update
@profile = ArtistProfile.find_by(params[:artist_id])
if @profile.update_attributes(profile_params)
redirect_to current_artist
else
redirect_to root_url
end
end
private
def set_artist
@artist = current_artist
end
def correct_artist
@artist = current_artist
if @artist != Artist.find(params[:id])
redirect_to artist_path
end
end
def profile_params
params.require(:artist_profile).permit(:biography, :location, :genre, :members, :facebook_url, :twitter_url, :youtube_url, :itunes_url, :amazon_url)
end
end
### /artist.rb ###
class Artist < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable, :timeoutable
has_one :artist_profile, dependent: :destroy
### /artist_profile.rb ###
class ArtistProfile < ActiveRecord::Base
belongs_to :artist
validates :artist_id, presence: true
end
I am putting my own code into the Devise Registrar in the create method. After registration, an ArtistProfile model is created and filled with an empty string that is the prefect. However, if I try to edit / update an individual artist profile, only the first profile profile is updated.
so Artist 1 subscribes and profile 2 is created. Artist 1 updates the 1 location profiles in Buffalo via the edit page. Artist 2 subscribes and profile 2 is created. Artist 2 updates Profile 2 in New York, but Profile 1 is updated, not Profile 2.
Is this the way to create the model on registration, and if so how should I fix the edit / update methods?
Or is there a better way at all?
source to share
This line of your code is wrong:
@profile = ArtistProfile.find_by(params[:artist_id])
The fix is ββto find the artist, then get the profile:
@profile = Artist.find(params[:artist_id]).artist_profile
Optimization:
@artist = Artist.find(params[:artist_id]).includes(:artist_profile)
@profile = @artist.artist_profile
Or, if your controller gets the id of the artist profile, you can do this:
@profile = ArtistProfile.find(params[:artist_profile_id])
source to share