ruby on rails - Displaying profile in view confusion -
so working on personal project learn rails nesting , decided use devise. created profile model , controller handle displaying other information user input @ later time. , utilized after_create :create_profile
handle heavy lifting. have setup in model , controllers information passed correctly, when comes displaying user's profile confusion arises.
this profile controller
class profilecontroller < applicationcontroller before_action :find_profile before_action :find_user before_action :authenticate_user! def new @profile = profile.new end def create @profile = current_user.create_profile(profile_params) end def show @profile = @user.profile end def update @profile.update_attributes(profile_params) if @profile.save redirect_to profile_path(current_user) end end private def find_user @user = user.find(params[:id]) end def find_profile @profile = profile.find(params[:id]) end def profile_params params.require(:profile).permit(:tagline, :age, :website) end end
in rails console, can manipulate data quite easily, in practical use (the view) find not case.
example being if want display second user's profile, in rails console, can simple this.
@u = user.find(params[:id])
find user, can @u.profile
grab associated profile.
since going looking users quite bit, made sense me create method , use before_action
. thought able following in view <%= @user.profile.tagline %> grab user's tagline tagline. that's not working out well. tried
@profile.user.tagline, displays nothing (no error thrown because added
attr_accessor` tagline on user model).
anyone have suggestions? hint i'm doing wrong?
the main problem can see is, using params[:id]
find both user
, profile
. assume 1 should correct.
so first try find out when id passing (this can figured link call profile page)
then try print both @user , @profile variables in page. can (in view ex)
#show.html.erb <%= @user.inspect %> <%= @profile.inspect %>
and if comfortable dig in more details, can use debugger gem pry
Comments
Post a Comment