0

I am trying to make a navbar in my rails 4 app.

When a user registers, I want to automatically build a profile for that user.

I have models for user and profile. The associations are:

user has_one profile
profile belongs_to user

I'm struggling with the automatic build, but I have a different question now.

In my navbar, I have a link to the users profile. I'm trying to figure out how to get that link to work.

I have routes as follows:

 resources :users do
     resources :profiles
  end

I have also tried my routes using profile (singular), as:

resources :users do
    resources :profile
end

In my navbar, I have:

<% if user_signed_in? %>
                    Hi <%= link_to(current_user.first_name.titlecase, user_profile_path(@current_user, @profile)) %></span>
                <span class="deviselinks" style="padding-right:30px">
                    <%= link_to "Sign out", destroy_user_session_path, :method => :delete %></span>

                  <% else %>

I have also tried without the '@' symbols, as:

Hi <%= link_to(current_user.first_name.titlecase, user_profile_path(current_user, profile)) %></span>

I have also tried:

Hi <%= link_to 'current_user.first_name.titlecase', [@current_user, @profile] %></span>

To me, this appears consistent with the rails guide example: http://guides.rubyonrails.org/routing.html

When I try this, I try creating a new user, but I get this error message:

ActionView::Template::Error (Nil location provided. Can't build URI.):
2015-12-28T21:08:51.228286+00:00 app[web.1]: 
2015-12-28T21:08:51.228279+00:00 app[web.1]:     11:               <ul style="text-align: right">
2015-12-28T21:08:51.228287+00:00 app[web.1]: 
2015-12-28T21:08:51.228280+00:00 app[web.1]:     12:                 <span class="deviselinks" style="padding-right:30px">
2015-12-28T21:08:51.228281+00:00 app[web.1]:     13:                   <% if user_signed_in? %>
2015-12-28T21:08:51.228282+00:00 app[web.1]:     14:                     Hi <%= link_to 'current_user.first_name.titlecase', [@current_user, @profile] %></span>
2015-12-28T21:08:51.228282+00:00 app[web.1]:     15: 

When I try the navbar path as:

Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@current_user, @profile) %>

I get this error:

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles", :id=>nil, :user_id=>nil} missing required keys: [:id, :user_id]):

Can anyone see how to set this up properly so the navbar links to the current users profile show page?

When I rake routes for grep profile, I get:

user_profiles GET       /users/:user_id/profiles(.:format)          profiles#index
                         POST      /users/:user_id/profiles(.:format)          profiles#create
        new_user_profile GET       /users/:user_id/profiles/new(.:format)      profiles#new
       edit_user_profile GET       /users/:user_id/profiles/:id/edit(.:format) profiles#edit
            user_profile GET       /users/:user_id/profiles/:id(.:format)      profiles#show
                         PATCH     /users/:user_id/profiles/:id(.:format)      profiles#update
                         PUT       /users/:user_id/profiles/:id(.:format)      profiles#update
                         DELETE    /users/:user_id/profiles/:id(.:format)      profiles#destroy

ANOTHER ATTEMPT

When I try changing the nav bar link to:

Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@user, @profile) %></span>

I get the same error as I set out above.

ANOTHER ATTEMPT

When I try changing the nav bar link to:

Hi <%= link_to 'current_user.first_name.titlecase', user_profile_path(@user_id, @profile_id) %></span>

I get the same error as I set out above.

ANOTHER ATTEMPT

When I try changing my routes to:

devise_for :users, #class_name: 'FormUser',
             :controllers => {
                :registrations => "users/registrations",
                # :omniauth_callbacks => "users/authentications"
                :omniauth_callbacks => 'users/omniauth_callbacks'
           }

  # get '/auth/:provider/callback' => 'users/authentications#create'
  # get '/authentications/sign_out', :to => 'users/authentications#destroy' 

  # PER SOURCEY TUTORIAL ----------
  match '/users/:id/finish_signup' => 'users#finish_signup', via: [:get, :patch], :as => :finish_signup
resources :users do
     resources :profiles, only: [:new, :create]
  end
resources :profiles, only: [:show, :edit, :update, :destroy]

And changing my nav bar link to:

 Hi <%= link_to 'current_user.first_name.titlecase', profile_path(@profile) %>

I get this error:

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles", :id=>nil} missing required keys: [:id]):

When I try the nav bar link as shown in the suggestion below:

Hi <%= link_to (current_user.first_name.titlecase, profile_path(@profile)) %></span>

I get this error:

SyntaxError (/app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ',', expecting ')'
2015-12-28T22:14:54.726458+00:00 app[web.1]: ...rent_user.first_name.titlecase, profile_path(@profile)) );@o...
2015-12-28T22:14:54.726459+00:00 app[web.1]: ...                               ^
2015-12-28T22:14:54.726460+00:00 app[web.1]: /app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ')', expecting keyword_end

When I try changing the nav bar link to:

            Hi <%= link_to ('current_user.first_name.titlecase', profile_path(@profile)) %></span>

I get another syntax error as:

SyntaxError (/app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ',', expecting ')'
2015-12-28T22:33:53.457021+00:00 app[web.1]: 
2015-12-28T22:33:53.457014+00:00 app[web.1]: ...ent_user.first_name.titlecase', profile_path(@profile)) );@o...
2015-12-28T22:33:53.457015+00:00 app[web.1]: /app/app/views/pages/_nav.html.erb:14: syntax error, unexpected ')', expecting keyword_end
2015-12-28T22:33:53.457015+00:00 app[web.1]: ...                               ^
2015-12-28T22:33:53.457016+00:00 app[web.1]: ...ase', profile_path(@profile)) );@output_buffer.safe_append='...
2015-12-28T22:33:53.457017+00:00 app[web.1]: ...                               ^):

ANOTHER ATTEMPT

When I try following the ideas in this post:

User profile pages with devise - Routing to show action

I change my navbar link to:

        Hi <%= link_to 'current_user.first_name.titlecase', current_user_profile_path %></span>

I then get this error message:

ActionView::Template::Error (undefined local variable or method `current_user_profile_path' for #<#<Class:0x007f4337749f60>:0x007f4337749358>):

ANOTHER ATTEMPT

When I try changing my navbar link to:

 Hi <%= link_to('current_user.first_name.titlecase', profile_path) %></span>

I then get this error message:

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"profiles"} missing required keys: [:id]):

I'm going to go crazy trying to learn to code.

NEW RAKE ROUTES FOR PROFILE IS:

edit_profile GET       /profiles/:id/edit(.:format)           profiles#edit
                 profile GET       /profiles/:id(.:format)                profiles#show
                         PATCH     /profiles/:id(.:format)                profiles#update
                         PUT       /profiles/:id(.:format)                profiles#update
                         DELETE    /profiles/:id(.:format)                profiles#destroy

           user_profiles POST      /users/:user_id/profiles(.:format)     profiles#create
        new_user_profile GET       /users/:user_id/profiles/new(.:format) profiles#new

When I start it up and press new user, the logs show this get command:

Started GET "/users/sign_up"
Community
  • 1
  • 1
Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

0

I'd suggest only nesting some profile actions, like this:

resources :users do
    resources :profiles, only: [:new, :create]
end

and then using top-level routes for the other actions:

resources :profiles, only: [:show, :edit, :update, :destroy]

If you have the current_user, you can pass just that user's profile to those actions instead of both. Of course, your path names will change and you'll have to change those in your views. (Something like Hi <%= link_to(current_user.first_name.titlecase, profile_path(@profile)) %>).

I'm curious, though, why have what appears to be a link to the user lead to the user's profile? Why not link to Users#show, using user_path(@current_user) or whatever your equivalent route is, and then just rendering the profile on the Users#show view?

elements
  • 1,047
  • 2
  • 9
  • 21
  • So how would I setup the navbar link with this approach? – Mel Dec 28 '15 at 22:01
  • @user2860931 Just added an example link for you to try. – elements Dec 28 '15 at 22:05
  • I tried this - chaining the routes and the link as you suggested. I get this error:(No route matches {:action=>"show", :controller=>"profiles", :id=>nil} missing required keys: [:id]): I'll copy it above so it's neater to read – Mel Dec 28 '15 at 22:08
  • To answer your question. I have a user model (for attributes that users won't change) and a profile model, for attributes that users may want to update. I don't have a view page for users. I just have one for profiles. So I want to go there. – Mel Dec 28 '15 at 22:12
  • @user2860931 You're getting the syntax error because you have a space between `link_to` and the parenthesis. What is your new `rake routes` output and when you click the new link, what is shown in your server logs (should start out with something like `Started GET ...`)? – elements Dec 28 '15 at 22:52
  • @user2860931 Actually, once you fix the syntax error, check to make sure that @profile is actually getting set. That seems like the most likely cause. Try this: `<%= link_to(current_user.first_name.titlecase, profile_path(current_user.profile)) %>` to see if that's the case. – elements Dec 28 '15 at 22:59
  • Hi, yes, I found the space. I added it because it's shown like that in the other link I posted. I'm still in all sorts of mess with the functionality of this. – Mel Dec 28 '15 at 22:59
  • Thank you! That way worked. I would love to understand how you knew to try that formulation. It doesn't look anything like what's in the rails guides. – Mel Dec 28 '15 at 23:05
  • It was just one way to try to get it working, and something that was used on a project I worked on previously. You could probably use your original routing and link and pass `current_user.profile` or make sure `@profile` gets set and use that and it should also work (like this answer and comments explain: http://stackoverflow.com/a/7029769/2599738). You mentioned something about going crazy learning to code...don't worry, that feeling is 100% to be expected. Just keep at it and you'll be fine. – elements Dec 28 '15 at 23:20