1
votes

I know there was a few questions like that but I couldn't find my case. I check that web site as well: https://github.com/bitmakerlabs/debugging-guide/wiki/Rails-error:-Routing-Error:-uninitialized-constant and it looks like it should work. Cannot find where is the problem.

Error looks like:

uninitialized constant ProfileController

In my app/controllers folder I have profiles_controller.rb file:

class ProfilesController < ApplicationController
  # GET to /users/:user_id/profile/new
  def new
    # Render blank profile details form
  end
end

I have a view file under app/views/profiles/new.html.erb and link to it with <%= link_to "Create your profile!", new_user_profile_path(user_id: current_user.id) if user_signed_in? %>. In my routes I have:

new_user_profile_path GET /users/:user_id/profile/new(.:format)
profile#new

Could you direct me to an error? Every answer I could find relates to naming while mine looks fine to me (profiles_controller.rb and ProfilesController class). Am I missing something obvious?

EDIT:

my routes.rb file snippet for profiles:

resources :users do
  resources :profile
end

EDIT 2:

In my project one user may have only one profile. It makes more sense to have route ...user/profile/... than ...user/profiles/...

2
can you add your routes.rb file's snippet for resource :users? - Anurag Aryan
@AnuragAryan I added that at the end of my question. - Malvinka
@RockwellRice it is called ProfilesController indeed and I believe it should be plural? But I noticed that error is about ProfileController however I don't know why... - Malvinka
@RockwellRice nope, it was 'profiles' folder since the beginning. But I had "profile" in the routes.rb instead of "profiles". It is solved, thank you! If you could add that comment as an answer I will check it as correct answer. - Malvinka

2 Answers

1
votes

You need to change the name of the route.

routes.rb

profiles#new
0
votes

There is another way to solve this problem. Because every user has one profile (that association is in my project however I didn't mention it as I didn't think it is relevant) in route.rb instead of changing "profile" to "profiles" it is enough to change "resources :profile" into "resource :profile". That way later on in our routes I have user/profile/... which makes more sense.