0
votes

I'm building a simple app that has a typical User model & a Profile model. User has_one Profile and Profile belongs_to User. All seems to be working fairly well as I am basically following the Michael Hartl tutorial. However, when I try to render the view of something from the profiles table (show action), I get an error (no id) AND the profile record I created gets deleted!

Questions:

  1. In my ProfilesController, am I defining my show action properly for the simple view I am trying to render?

  2. Why does simply visiting the url localhost/3000/profiles/1 delete the profile record? I think it has something to do with dependent destroy (b/c removing that will stop this behavior), but I think I want to keep dependent destroy, correct?

Routes

resources :users
resources :profiles

Models

Class User < ActiveRecord::Base
has_one :profile, dependent: :destroy

Class Profile < ActiveRecord::Base
belongs_to :user

ProfilesController

def new
  @profile = current_user.build_profile
end

def create
  @profile = current_user.build_profile(params[:profile])
  if @profile.save
    flash[:success] = "Profile created dude!"
    redirect_to root_path
  else
    render 'new'
  end
end

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

View (profiles/show.html.erb)

<p>Display Name:  <%= @profile.display_name %></p>
1

1 Answers

0
votes

Check your rake routes. You will see that for your Profile#show, you have URL structure like: /profiles/show/:id.
Thus, params, must be expecting the :id instead of :user_id.

If by /profiles/show/3, you wish to show profile 3, then:

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