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"