Ruby on Rails - routing error, goes to "show" instead of "index" - ruby-on-rails

I have a basic homepage with a link that I would like to go to businesses/index.html.erb
<%= link_to("Go to businesses index page", {:controller=>"businesses", :action =>"index"}) %>
When I click on this link I get a routing error:
No route matches {:action=>"show", :controller=>"businesses"}
My routes.rb file looks like:
RailsProject::Application.routes.draw do
get "welcome/index"
root :to => 'welcome#index'
get "businesses/index"
resources :businesses
end
and rake routes gets me:
welcome_index GET /welcome/index(.:format) welcome#index
businesses GET /businesses(.:format) businesses#index
POST /businesses(.:format) businesses#create
new_business GET /businesses/new(.:format) businesses#new
edit_business GET /businesses/:id/edit(.:format) businesses#edit
business GET /businesses/:id(.:format) businesses#show
PUT /businesses/:id(.:format) businesses#update
DELETE /businesses/:id(.:format) businesses#destroy
root / welcome#index
businesses_index GET /businesses/index(.:format) businesses#index
I am using rails version 3.2

The problem was that in my businesses/index there was a line reading
<%= link_to 'show', business_path %>
that should have been
<%= link_to 'show', business_path(business) %>

Related

I am working on An Ruby on Rails app

I am working an an Rails app. The problem I am have is when I cycle between my about and contact pages. I always get the error
No route matches [GET] "/pages/pages/about"
or
No route matches [GET] "/pages/pages/contact"
I'm trying to change the routes my nav bar partial the tag href to "/about" but the same error occur. it ask me to use the command rake routes and it shows
$ rake routes
restaurants GET /restaurants(.:format) restaurants#index
POST /restaurants(.:format) restaurants#create
new_restaurant GET /restaurants/new(.:format) restaurants#new
edit_restaurant GET /restaurants/:id/edit(.:format) restaurants#edit
restaurant GET /restaurants/:id(.:format) restaurants#show
PUT /restaurants/:id(.:format) restaurants#update
DELETE /restaurants/:id(.:format) restaurants#destroy
pages_about GET /pages/about(.:format) pages#about
root / restaurants#index
pages_contact GET /pages/contact(.:format) pages#contact"
can some one please help me!!
Your about and contact are under the url /pages/about and /pages/contact but the URL you are accessing is /pages/pages/about which doesn't exist.
You can see in the rake routes, the possible urls in the system.
you need to have the link in your web app as
<%= link_to 'About', pages_about_path %>
<%= link_to 'Contact', pages_contact_path %>
in your routes you can do something like
#config/routes.rb
ClientCruiser::Application.routes.draw do
....
match "contact" => "pages#contact", :as => :contact, via: :all
match "about" => "pages#about", :as => :about, via: :all
....
root :to => 'pages#index'
end
The output of this
contact /contact(.:format) pages#contact
about /about(.:format) pages#about
root GET / pages#index
the way you would make the call would be
<%= link_to about_path, "About", class: '' %>
<%= link_to contact_path, "Contact", class: '' %>

Rails: Linking from Subdomain to Main Domain and Back

I have an app with subdomains for various people that all share the same domain (i.e. www.liz.domain.com, www.anthony.domain.com, etc.). I need to create links from one subdomain to the main domain and back, but can't figure out how to make it work. My routes stand like this:
Rails.application.routes.draw do
constraints subdomain: 'liz' do
scope module: 'liz', as: 'liz' do
get 'home/index'
root 'home#index'
resources :inquiries
get 'services/hire'
get 'services/dev'
get 'services/design'
get 'services/branding'
get 'services/portfolio'
end
end
constraints subdomain: 'anthony' do
scope module: 'anthony', as: 'anthony' do
get 'home/index'
root 'home#index'
end
end
get 'home/index'
root 'home#index'
end
And when I rake routes it turns out like this:
Prefix Verb URI Pattern Controller#Action
liz_home_index GET /home/index(.:format) liz/home#index {:subdomain=>"liz"}
liz_root GET / liz/home#index {:subdomain=>"liz"}
liz_inquiries GET /inquiries(.:format) liz/inquiries#index {:subdomain=>"liz"}
POST /inquiries(.:format) liz/inquiries#create {:subdomain=>"liz"}
new_liz_inquiry GET /inquiries/new(.:format) liz/inquiries#new {:subdomain=>"liz"}
edit_liz_inquiry GET /inquiries/:id/edit(.:format) liz/inquiries#edit {:subdomain=>"liz"}
liz_inquiry GET /inquiries/:id(.:format) liz/inquiries#show {:subdomain=>"liz"}
PATCH /inquiries/:id(.:format) liz/inquiries#update {:subdomain=>"liz"}
PUT /inquiries/:id(.:format) liz/inquiries#update {:subdomain=>"liz"}
DELETE /inquiries/:id(.:format) liz/inquiries#destroy {:subdomain=>"liz"}
liz_services_hire GET /services/hire(.:format) liz/services#hire {:subdomain=>"liz"}
liz_services_dev GET /services/dev(.:format) liz/services#dev {:subdomain=>"liz"}
liz_services_design GET /services/design(.:format) liz/services#design {:subdomain=>"liz"}
liz_services_branding GET /services/branding(.:format) liz/services#branding {:subdomain=>"liz"}
liz_services_portfolio GET /services/portfolio(.:format) liz/services#portfolio {:subdomain=>"liz"}
anthony_home_index GET /home/index(.:format) anthony/home#index {:subdomain=>"anthony"}
anthony_root GET / anthony/home#index {:subdomain=>"anthony"}
home_index GET /home/index(.:format) home#index
root GET / home#index
I've tried using a <%= link_to "Liz's Page", liz_root_path(subdomain: 'liz') %> or <%= link_to "Liz's Page", liz_root_path %> from the main domain (without a subdomain), but neither links to the subdomain.
Similarly, I've tried <%= link_to "Main Page", root_path %> from a page inside a subdomain and it just links to the subdomain's home, not the site root.
Can anyone straighten me out on how to link back and forth between subdomains/main domains?
I eventually changed these from path to url to solve the problem.
This way <%= link_to "Liz's Page", liz_root_path(subdomain: 'liz') %> became <%= link_to "Liz's Page", liz_root_url(subdomain: 'liz') %> and worked fine.

<Ruby on Rails> Routes

I'm a beginner in ruby on rails and was following an online tutorial. I got lost immediately on routes. Every time I try to go to my url 'pages/help' an error is displaying in the browser "The action 'show' could not be found for PagesController". I have a ruby partitions named _header that contains these codes:
<header>
<%= link_to logo, root_path %>
<ul class="nav nav-tabs">
<li class="active">
<%= link_to "Home |", root_path %>
</li>
<li><%= link_to "Help |", pages_help_path %></li>
<li><%= link_to "Sign In", '#'%></li>
</ul>
</header>
And when I try to click the "Help" link the error occurs.
My routes.rb contains just these codes:
BakeShop::Application.routes.draw do
resources :pages
root :to => 'pages#home'
#match '/help', :to => 'pages#help'
get "pages/help"
end
and i have a PagesController with only these codes:
class PagesController < ApplicationController
def home
end
def help
end
end
and when I run 'rake routes' the lists contained are:
pages GET /pages(.:format) pages#index
POST /pages(.:format) pages#create
new_page GET /pages/new(.:format) pages#new
edit_page GET /pages/:id/edit(.:format) pages#edit
page GET /pages/:id(.:format) pages#show
PATCH /pages/:id(.:format) pages#update
PUT /pages/:id(.:format) pages#update
DELETE /pages/:id(.:format) pages#destroy
root GET / pages#home
pages_help GET /pages/help(.:format) pages#help
And I certainly have home.html.erb and help.html.erb created in my views folder. And the twist is that when I create a 'show.html.erb' in my views folder and create an action in my pages controller named 'show', the error disappears and links to .
So what I'm saying is, can anyone explain this? Why is rails looking for 'show' action, and not 'help' action, that I didn't define
One thing to understand about routes in Rails is that the order of routes specified is very important. In your config/routes.rb file you have routes specified in the following order:
resources :pages
root :to => 'pages#home'
get "pages/help"
So, what this does is, matches the routes defined with resources :pages first. As you can see in the output of rake routes, Rails generated the following routes for that specific line i.e. resources :pages:
pages GET /pages(.:format) pages#index
POST /pages(.:format) pages#create
new_page GET /pages/new(.:format) pages#new
edit_page GET /pages/:id/edit(.:format) pages#edit
page GET /pages/:id(.:format) pages#show
PATCH /pages/:id(.:format) pages#update
PUT /pages/:id(.:format) pages#update
DELETE /pages/:id(.:format) pages#destroy
Also note that pages_help GET /pages/help(.:format) pages#help is at the bottom of that list. So what is happening is when you navigate to /pages/help, Rails finds the first GET request that matches that pattern, in this case:
page GET /pages/:id(.:format) pages#show
And since you don't have the show action defined, it's throwing that error "The action 'show' could not be found for PagesController".
So, to solve you specific problem without the show action and show.html.erb, you can rearrange the config/routes.rb as follows:
BakeShop::Application.routes.draw do
get "pages/help" # Move this line before resources :pages
resources :pages
root :to => 'pages#home'
end
Moving get "pages/help" will ensure that for your url /pages/help this route will match and your help.html.erb will be rendered.
Update:
How it matched pages/show?
# In the comments below where .format is used think of various extensions just as html, js, json, xml etc but not required.
pages GET /pages(.:format) pages#index
# Matches /pages, /pages.format
new_page GET /pages/new(.:format) pages#new
# Matches /pages/new, /pages/new.format
edit_page GET /pages/:id/edit(.:format) pages#edit
# Matches /pages/1/edit, /pages/1/edit.format or pages/anything/edit.format. Note that the edit in the end is required here,the only variable here is id and format
page GET /pages/:id(.:format) pages#show
# Matches /pages/1, /pages/anything, /pages/1.format, /pages/anything.format. In the question /pages/help matches.
You have to enable the default root in your routes.rb.
Update routes.rb:-
BakeShop::Application.routes.draw do
resources :pages
root :to => 'pages#home'
#match '/help', :to => 'pages#help'
get "pages/help" #this is not required
match ':controller(/:action(/:id(.:format)))'
end
The problem here is that the route for the show action (page_path) and the route for your pages_help_path conflict. If you simply move the line get "pages/help" before the line resources :pages it will work as you intend.
Routes.rb:
BakeShop::Application.routes.draw do
get "pages/help"
resources :pages
root :to => 'pages#home'
end

No route matches error but rake routes finds it, Rails.application.routes recognises it

Strangely when I include "resources :blogs" to my routes.rb I get the following error for any other path I try to reach:
No route matches {:action=>"show", :controller=>"blogs"}
The only path I don't get this error for is the "/blogs/:id(.:format)" itself.
Rake routes ("show" for "blogs" in line 5):
blogs GET /blogs(.:format) blogs#index
POST /blogs(.:format) blogs#create
new_blog GET /blogs/new(.:format) blogs#new
edit_blog GET /blogs/:id/edit(.:format) blogs#edit
blog GET /blogs/:id(.:format) blogs#show
PUT /blogs/:id(.:format) blogs#update
DELETE /blogs/:id(.:format) blogs#destroy
root / pages#home
home /home(.:format) pages#home
products /products(.:format) pages#products
services /services(.:format) pages#services
research /research(.:format) pages#research
/blog(.:format) pages#blog
contact /contact(.:format) pages#contact
On the other hand when tried in console there is no such error for "/products" (for instance) which in the browser returns the "No route matches" error:
1.9.3p327 :010 > Rails.application.routes.recognize_path "/products"
=> {:controller=>"pages", :action=>"products"}
1.9.3p327 :011 > Rails.application.routes.recognize_path "/blogs/1"
=> {:action=>"show", :controller=>"blogs", :id=>"1"}
Any idea?
UPDATE:
Here is my blogs_controller.rb:
class BlogsController < ApplicationController
def show
#blog = Blog.find(params[:id])
end
def new
end
end
and my show.html.erb of the blogs:
<%= #blog.title %>, <%= #blog.description %>
So far this is all I have developed to test my Blog model.
Most likely, you have something like
<%= link_to "Blogs", blog_path =>
in layout template. If so, correct it to <%= link_to "Blogs", blogs_path =>

ActionController::MethodNotAllowed (Only get, put, and delete requests are allowed.)

I'm getting this error, ActionController::MethodNotAllowed (Only get, put, and delete requests are allowed.), when I try to submit a form. My route looks like this,
admin.resources :email_launcher
and this is my form_for,
form_for :email_launcher, :url => new_admin_email_launcher_path, :method => :get do |f|
This is not in a form but in the new view, and I'm using HAML. I've google searched all day on the error but never found anything of value. Any help would be great, thanks.
Here's my rake routes for admin_email_launcher
admin_email_launcher_index GET /admin/email_launcher(.:format) {:controller=>"admin/email_launcher", :action=>"index"}
POST /admin/email_launcher(.:format) {:controller=>"admin/email_launcher", :action=>"create"}
new_admin_email_launcher GET /admin/email_launcher/new(.:format) {:controller=>"admin/email_launcher", :action=>"new"}
edit_admin_email_launcher GET /admin/email_launcher/:id/edit(.:format) {:controller=>"admin/email_launcher", :action=>"edit"}
GET /admin/email_launcher/:id(.:format) {:controller=>"admin/email_launcher", :action=>"show"}
PUT /admin/email_launcher/:id(.:format) {:controller=>"admin/email_launcher", :action=>"update"}
DELETE /admin/email_launcher/:id(.:format) {:controller=>"admin/email_launcher", :action=>"destroy"}
routes.rb
namespace :admin do
resources :email_launcher
end
admin/email_launchers_controller.rb
def new
#email_launcher = EmailLauncher.new
end
admin/email_launcher/new
<%= form_for([:admin,#email_launcher]) do |f| %>
...
<%= f.submit %>
<% end -%>
It will POST new email_launcher.
Why do you need to GET the new action? You're already on the new page.

Resources