So I am currently trying to figure out what path to use so when the user clicks on 'View Profile' the link will be domain.com/USERNAME instead of domain.com/profiles/show
My current code for the link is
<li><%= link_to "View Profile", profiles_show_path %></li>
my routes.rb is set at
get '/:id' to: 'profiles#show'
You need to specify an user as parameter in your link:
<li><%= link_to "View Profile", profile_path(user) %></li>
In your routes:
get '/:id', to: 'profiles#show', as: :profile
So you're overriding the default profile_path().
Related
Been trying to find out a better way to link to pages that contains /:id's in them. Because the way I'm doing it now works but can easily break if I change something.
Currently I have two of these examples. One to link to your profile done like this:
<li><%= link_to "Your profile", root_url + "users/#{current_user.id}" %></li>
and the other:
<%= link_to "Apply now", root_url + "answers/#{f.id}" %>
how do I make it so that I don't have to rely on the manual change of this. On my rake routes I cant write 'answers_path/#{f.id}' for example as it doesn't show me any path.
So how do I do this? My routes setup for these two are currently:
match '/answers/:id', to: 'answers#show', via: 'get'
match '/users/:id', to: 'users#show', via: 'get'
Instead of using match I would define resources routes
resources :users, :answers
Now you can use:
<%= link_to 'Your Profile', current_user %>
<%= link_to 'Apply Now', f %>
You can learn more about rails path helpers and resource routing here - http://guides.rubyonrails.org/routing.html
Also, if you don't need all the CRUD actions, you can use only:
resources :users, :answers, only: :show
The following should do it for you.
<li><%= link_to "Your profile", user_path(current_user.id) %></li>
and
<%= link_to "Apply now", answer_path(f.id) %>
I do, however, recommend you review the following Rails Guide: http://guides.rubyonrails.org/routing.html
So, I have the following code. I want to access timespans_path,
but I can't.
<% content_for :div_header do%>
<h1> Welcome, <%= #l_user.name %> </h1>
<% end %>
<% content_for :div_sub_header do %>
<ul>
<li><%= link_to "show entries", entries_path %></li>
<li><%= link_to "show groups", groups_path %>
<% if can? :read, Subgroup %>
,
<%= link_to " subgroups", subgroups_path %>
</li>
<% end %>
<li><%= link_to "show users", users_path %></li>
<li><%= link_to "show actioncodes", actioncodes_path %></li>
<li><%= link_to "show timespans", timespans_path %></li>
</ul>
<% end %>
I always get these errors:
NameError in Application#welcome
Showing C:/xampp/htdocs/fluxcapacitor/app/views/application/welcome.html.erb where line #16 raised:
undefined local variable or method `timespans_path' for #<#<Class:0x58b8610>:0x58b7e18>
This is my route.rb:
Fluxcapacitor::Application.routes.draw do
root 'application#welcome'
get 'login' => 'application#login'
post 'login' => 'application#process_login'
post '' => 'application#process_login'
post 'send_request_account_mail' => 'application#send_request_account_mail'
post 'send_forgot_password_mail' => 'application#send_forgot_password_mail'
get 'forgot_password' => 'application#forgot_password'
get 'request_account' => 'application#request_account'
get 'welcome' => 'application#welcome'
get 'logout' => 'application#logout'
if Rails.env.development?
get 'display_mail' => 'application#display_mail'
end
resources :users
get 'multiple_new' => 'users#multiple_new'
post 'multiple_new' => 'users#multiple_new'
post 'multiple_create' => 'users#multiple_create'
get 'users/:id/:hash/cal' => 'users#cal'
resources :actioncodes
resources :entries
resources :timespans
resources :groups do
member do
get 'search_admin'
post 'search_admin'
post 'add_admin'
get 'remove_admin'
post 'remove_admin'
end
end
resources :subgroups do
member do
get 'search_user'
post 'search_user'
post 'add_user'
get 'remove_user'
post 'remove_user'
get 'remove_admin'
post 'remove_admin'
end
end
end
Why am I getting the error? How can I fix it?
Add
resources :timespans
in your route.rb
It looks like you haven't defined a path called timespans, so the view doesn't know what URL to render and is raising an error.
If you have a model called Timespan then adding resources :timespans to your routes file will create a path called timespans_path (among others), pointing to /timespans.
You can also create any arbitrary path called timespans path using the :as option, e.g.:
get "/the_url", to: "the_controller#the_action", as: :timespans
If you have problems with paths in the future, note that you can use the rake task rake routes to list all your generated routes and the names of their path helpers, which can be very helpful for debugging.
I'm using devise for user authentication. In my views I've set:
<% if user_signed_in? %>
<li><%= link_to "Log Out", destroy_user_session_path %></li>
<% else %>
<li><%= link_to "Sign In", new_user_session_path %></li>
<% end %>
However when I click Log_Out I'm getting an error:
No route matches [GET] "/users/sign_out"
However when I check my rake routes I'm getting:
devise/sessions#destroy destroy_user_session DELETE /users/sign_out(.:format)
What apneadiving said.
<%= link_to "Log Out", destroy_user_session_path, method: :delete %>
Default sign out is use "delete" method. Your route also said the method is “DELETE"
If you want use "get" method
Modify devise.rb to
config.sign_out_via = :get
Baloo is right, make sure to use the :delete method. You can see this clearly if you invoke
rake routes
You'll see the path as well as the method.
So I'm trying to Change in the menu the field "Profile" to the User's name.
<li><%= link_to "Profile", "#" %></li>
So i changed it to
<li><%= link_to User.name, "#" %></li>
but now in the Menu, it states "User", and not the User's Name.
Any Solution?
Thank you
Assuming you get your currently logged in user via current_user method, it should be:
<li><%= link_to current_user.name, '#' %></li>
What you are doing now is sending name message to User class instead of User instance. Since User class doesn't have name method defined, you get this error.
I have a rails 3 app, and when I click the link to my terms page, it routes to a completely different controller, than what the routes should use. Stranger still, the route works when I'm not logged in, and I'm using devise.
I get this error when clicking the link when I'm logged in.
No route matches {:action=>"edit", :controller=>"users", :id=>nil}
<%= link_to "Terms", terms_path %>
Routes (in the order they appear in routes.rb):
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users do
member do
get :following, :followers
post :accept
end
end
match '/terms', to: 'static_pages#user_agreement'
Static Pages Controller
def user_agreement
end
Rake Routes
terms /terms(.:format) static_pages#user_agreement
This also happens for every other action that I've routed this way to the staticpages controller, but not for any other actions that route to different controller.
Update: Terms Page
Header:
<%= link_to "Follow", users_path %>
<%= link_to current_user.name, current_user %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
Footer:
<%= link_to "Welcome", welcome_path %>
<%= link_to "Settings", edit_user_path(#user) %>
<%= link_to "Terms", terms_path %>
All the content is pure html.
Thanks in advance
You have a link to edit_user_path with no #user as hinted in the comments.
You should almost certainly be using current_user anyway.