I've got an app where the links work fine when I'm on the homepage, but when I'm on a page belonging to a resource such as "users", some links get a little weird.
When I'm on the homepage and I click the link for the "About" page, I'm taken there directly. But if I'm on the page for users/index and I hover over the link for the "About" page, it shows the destination as being "users/about."
Here's what my routes file looks like.
RobotimusApp::Application.routes.draw do
resources :users
root to: "pages#home"
match '/about', to:'pages#about'
match '/guides', to:'pages#guides'
end
Here's what my nav bar looks like
%ul.nav
%li
=link_to 'Home', root_url
%li
=link_to 'About', 'about'
- if user_signed_in?
%li
= link_to('My Sites', user_path(current_user))
Welp, the issue lies in this line:
=link_to 'About', 'about'
Instead of passing in a routed path like about_path, we pass in the string 'about'. When link_to receives a string as its URL parameter, it doesn't run it through the router or anything; it assumes that that's the URL you want to go to. (After all, about_path and the like return strings.)
So, you end up with something like this in the HTML:
About
Here, about is a relative URL, so from the path /users it will go to /users/about.
You could just use a preceding slash to make it an absolute path:
= link_to 'About', '/about'
But it's not particularly Rails-y to hard-code our URLs when we can use routes instead. You're probably going for something like this:
= link_to 'About', about_pages_path
(Is about_pages the route name? It probably should be. You can always name it with an as parameter.)
If you run rake routes in a terminal you'll get a list of all the routes available.
think the link for your about page should be
= link_to 'About', about_pages_path
Related
I am having difficulties with routings in ruby on rails. I have same url format in different controllers and I am trying to send params to site controller but it goes to pages controller instead. Any help?
Route.rb
resources :sites, :path => '', :except => [:index] do
resources :pages, :path => '' do
end
end
When I run rails routes command I am getting something look like:
site_pages GET /:site_id(.:format) pages#index
site GET /:id(.:format) sites#show
View
<%= link_to 'GO', site_path(site) %>
This code should go to the site#show but it goes to pages#index and it gives me this error message below:
NoMethodError in PagesController#index
undefined method `site_id' for nil:NilClass
Parameters:
{"site_id"=>"site A"}
I have tried:
<%= link_to '', site_path(site), :controller => "sites", :action => "show"%>
<%= link_to '', site_path(id: site.id)%>
but it doesn't work. Any help will be very appreciate
In addition to #jvillian's comment, your routes aren't functional.
Imagine an user wants to see the site with id 1. She could type in the URL my_domain.com/1. Now, she wants to check that site's pages, so she types in my_domain.com/1. How can Rails know what is she referring to?
Since pages is nested inside of sites, you'll (usually) need to have a site to have pages. To accomplish this in your scenario, remove the :path => '' from the pages resource. This will give you routes that look like this:
site_pages GET /:site_id/pages(.:format) pages#index
site GET /:id(.:format) sites#show
Then, you'll need to have a site id anytime you want to access/create its pages. Keep in mind, though, that if you pass a route like mydomain.com/anything_here, you're going to get directed to the sites#show action.
I am beginner to ror, I am trying dynamic page in my application, while I click the about link in my application I got a routing error(as said in title). Here is my routes.rb file
Rails.application.routes.draw do
devise_for :users
resources :userdetails do
collection {post :import}
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: "userdetails#index"
get 'about', to:'userdetails#about'
devise_scope :users do
get 'sign_in', to: 'devise/sessions#new'
get 'sign_out', to: 'devise/sessions#destroy'
end
end
here is index.html.erb file:
<div class="topnav">
Home
News
Contact
<a href="about_path" data-method = "get" >About Us</a>
...
</div>
Please help me identify my mistake.....
your routes says: -
get '/about', to: 'userdetails#about', as: :user_about
so according to this
About Us
Instead of this you can use rails view helper which fill convert this to html anchor tag: -
<%= link_to 'About Us', user_about_path%>
As you are beginner so you should go though this rails Routing for better clarification.
Note: - by default link is GET type http verb so you don't need to mention data-method = "get"
and you can see all your routes at console by running command rake routes
When you run rails routes or rake routes it will generate like this
about GET /about(.:format) userdetails#about
That means your path is about_path then you can call out like this
About Us
or like this HTML
About Us
or like this .erb
<%= link_to 'About Us', about_path %>
While your routes like exist
get 'about', to:'userdetails#about'
Here you have defined method get that's why this is unnecessary to HTML call out data-method = "get".
Hope it will work
Introduction
If you make this route on Rails...
Rails.application.routes.draw do
get 'help' => 'static_pages#help'
and later on you use the url helper, for instance
<%= link_to "Help", help_path %>
You will basically have a link to http://example.com/help
Actual problem
So far, it is alright. But when I create this route...
Rails.application.routes.draw do
get '321' => 'static_pages#help'
How would I create links to that page?
I create a URL that, typed manually, actually works: http://example.com/321. It is a valid URL. But I can't user 312_path, since that is not a valid method name.
I am implementing an admin subdomain and have googled to try and find the answer to this, however I have not found another instance.
My routes look like this for the subdomain section:
constraints :subdomain => 'admin' do
scope :module => "admin" do
resources :news, :events
match 'news', :to => 'news#index', :as => 'news'
root :to => "dashboard#index"
end
end
Events works fine, but for some reason in order for news to work I need to add a specific route to match it. It may help to show the partial where the error is generated (admin/shared/menu):
<ul>
<li><%= link_to 'Home', root_path, :class => "#{current_class?(root_path)}" %></li>
<li><%= link_to 'News', news_path, :class => "#{current_class?(news_path)}" %></li>
<li><%= link_to 'Events', events_path, :class => "#{current_class?(events_path)}" %></li>
<div class="clearboth"></div>
</ul>
And then the error if I was to remove the match route:
No route matches {:action=>"show", :controller=>"admin/news"} missing required keys: [:id]
I just don't have a clue why, any thoughts?
I found that this is an issue with rails in that the naming convention news should not be used due to plural issues, news -> new so therefore I had to rename everything to articles instead. Oversight on my part, a tad stupid.
The reason Rails gets confused is because "news" is used for both the singular and plural. news'.singularize gives news; and news.pluralize also gives news (Wikipedia has a longer description of this phenomenon; there are other words that do the same).
Rails will generate a news_path route (plural, for the index action) and a news_path route (singular, for the show action).
The singular route expects a News instance; it appears that the show action is defined later, and overwrites the index action, resulting in the strange behaviour.
The solution is simple: use the news_index_path if you want the index action. I haven't encountered any other issues, and am still using the News model.
ActiveAdmin fixed this issue in the same way.
If you really don't want to use News, then you can suffix it with Item, Entry, Object, or a similar word. This will leave you with a NewsItem model with news_item_path and news_items_path.
I'm following this tutorial (seems good) for Rails. After I run
ruby script/generate scaffold Post
then this link works in one of the erb files:
<%= link_to "My Blog", posts_path %>
WHY? I've looked for "posts_path" in the whole app and it's nowhere to be found. On the other hand, this
<%= link_to "My Blog", home_path %>
does not work, and it's also a Controller.
Where is the posts_path defined?
posts_path is a named route you get for free from the route that was added by script/generate scaffold. See routes.rb you should see something like this:
map.resources :posts
See the API docs for information on what other named routes you get for free.
Also you can run rake routes and see what all your routes.rb is giving you.
If you want a home_path named route add a line like this to your routes.rb:
map.home '/home', :controller => "home", :action => "index"
I believe that "posts_path" is created dynamically by Rails at runtime. Look at your routes.rb file - Home is probably not defined the same way as Posts. It has nothing to do with you controllers, it's dependent on the route definition.
map.root :controller => "home" would be a shorter way of writing the path to your home directory. This will use / has the home, and not /home. If you still want to use /home (and home_path), map.home 'home', :controller => "home" will do the same thing.
There's a great guide written by Mike Gunderloy about everything there is to know about routing.