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.
Related
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
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
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 am working in Ruby on rails.. i am new to this..
i have used a line
<%= link_to "about",about_path %>
which throws me a error as,
undefined local variable or method `about_path' for #<ActionView::Base:0xb5f5baa8>
i am having the page about.html under app/views/pages/
please give some suggestions of why i am getting like this .
I just had the same problem using the Hartl's tutorial. Here is what I did.
When asking rake routes, I have :
tomsihap-MBP:sample_app tomsihap$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
static_pages_about GET /static_pages/about(.:format) static_pages#about
static_pages_contact GET /static_pages/contact(.:format) static_pages#contact
Then the correct path is :
<%= link_to "About", static_pages_about_path %>
And not <%= link_to "About", about_path %> as suggested by Hartl's guide.
EDIT :
Ok now I understand. That is because routes were defined like that :
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
Instead of, later explained into the tutorial :
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
Using this way, the correct path now is :
<%= link_to "About", about_path %>
Your code is looking for what is called a named route. You need to define these in config/routes.rb. In addition you will need some controller and action to handle them. See this post describing a very simple way to handle static pages by way of illustration.
To get the about_path named route, then you would add this to routes.rb
map.about "/pages/about", :controller => "pages", :action => "show", :id => "about"
Then add your about page contents to a file called app/views/pages/about.html.erb
Finally:
$ rake routes
tells you all of the named routes defined for your application and what they do
I guess your about page is "static". Check this..
routes.rb
# rails 2.3.x
map.about "/pages", :controller => 'pages', :action => 'about'
Controllers/pages_controller.rb
class PagesController < ApplicationController
def about # not needed, only for "tidiness"
end
end
... and your erb file have to be here: Views\pages\about.html.erb
Is in your routes.rb something like map.resources :about ?
If you don't know why it should be there or what is that, read about RESTful Routing on guides.
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.