Ask A Question

Notifications

You’re not receiving notifications from this thread.

Did I forget to do a migration?

Christopher Guller asked in Rails

I have a profile model and controller in Rails, and it works all well and good except when I call profile_path or simply "profile". I try and create:

<%= link_to current_user.profile_path %>

and instead of loading a link to the current user's profile path, I get:

 PG::UndefinedTable: ERROR:  relation "profiles" does not exist
LINE 8:  WHERE a.attrelid = '"profiles"'::regclass

Also, in my schema.rb I have nothing mentioning any to profile or profiles. I'm not sure if this helps, but here are my models and controllers.
profile.rb

class Profile < ApplicationRecord
  belongs_to :user
end

user.rb

class User < ApplicationRecord
  has_one :profile dependent: :destroy
end

profile_controller.rb

class ProfileController < ApplicationController
  skip_before_action :configure_permitted_parameters, only: [:show]
  before_action :authenticate_user!, only: [:index, :follow, :unfollow]
  def show
    @user = User.find(params[:id])
  end
  def index
  end
end

Also a closer step to my issue is that I'm using a "notifications" gem (literally called that) for my notifications and this is what I have under it.

  # Method name of user profile page path, in User model, default: nil
  self.user_profile_url_method = 'profile'

Also, I think this might be a database problem and can probably be fixed with a simple migration, but I'm not sure what to put in the migration to fix it.

Reply

Hey Christopher,

path helpers like profile_path are not methods on the User model. You can use <%= link_to "Profile", profile_path %> in your view to link to a route that matches that name.

The Rails guides would be a good place to read through how they work: https://guides.rubyonrails.org/routing.html#naming-routes

This doesn't (and shouldn't) have anything to do with your database. The error makes it look like that because you were trying to call a method on a database object.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.