I am working on An Ruby on Rails app - ruby-on-rails

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: '' %>

Related

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 - routing error, goes to "show" instead of "index"

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) %>

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.

What is wrong with my routes?

root :to => "index#home"
#public tattoo viewing and submissions
match "/submit" => "index#new", :via => :get
match "/tattoo" => "index#create", :via => :post
match "/tattoo/:id" => "index#show", :via => :get
match "/tagged" => "index#tagged", :via => :get
match "/tattoo/:id" => "index#destroy", :via => :delete
match "/tattoos" => "index#index", :via => :get
members section and its nested images
resources :members, :except => [:new, :create] do
resources :tattoos
end
Thats whats in my routes.rb file. They produce:
root /(.:format) {:controller=>"index", :action=>"home"}
submit GET /submit(.:format) {:controller=>"index", :action=>"new"}
tattoo POST /tattoo(.:format) {:controller=>"index", :action=>"create"}
GET /tattoo/:id(.:format) {:controller=>"index", :action=>"show"}
tagged GET /tagged(.:format) {:controller=>"index", :action=>"tagged"}
DELETE /tattoo/:id(.:format) {:controller=>"index", :action=>"destroy"}
tattoos GET /tattoos(.:format) {:controller=>"index", :action=>"index"}
members GET /members(.:format) {:action=>"index", :controller=>"members"}
edit_member GET /members/:id/edit(.:format) {:action=>"edit", :controller=>"members"}
member GET /members/:id(.:format) {:action=>"show", :controller=>"members"}
PUT /members/:id(.:format) {:action=>"update", :controller=>"members"}
DELETE /members/:id(.:format) {:action=>"destroy", :controller=>"members"}
But i have a problem. For some reason, when I try to go to mysite.com/submit
I used to get this error
No route matches {:controller=>"images"}
on
<%= form_for #tattoo, :html =>{:multipart => true} do |f| %>
but that has magically changed to:
undefined method `images_path'
on the same line.
when my controller has this:
indexcontroller
def new
#tattoo = Image.new
end
def create
#tattoo = Image.new(params[:image])
if #tattoo.save
flash[:success] = "Tattoo sent in for approval!"
redirect_to(images_path)
else
render :action => "new"
end
end
And then this link_to:
<%= link_to "Manage tattoos", member_tattoos_path() %>
give me this error:
No route matches {:controller=>"tattoos"}
I thought I was beginning to understand routes and had a decent grasp but I dont get whats going on!
You need to pass in a member object to edit_member_path.
<%= link_to "Edit profile", edit_member_path(#member) %>
edit_member_path should know the id of the member you want to edit. Please try
<%= link_to "Edit profile", edit_member_path(#member) %>
For No route matches {:controller=>"images"}; since the action image is not defined in your route, please try to stop and restart the server and check if there is any plugin like Paperclip in place.

RoR routing very basic problem

I have Exam controller.
In routes.rb there is "resources :exams"
In controller there are REST-like generated methods.
I want to add my own method there:
def search
#exams = Exam.where("name like ?", params[:q])
end
In view file:
<%= form_tag(search_path, :method => "post") do %>
<%= label_tag(:q, "Szukaj: ") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Szukaj") %>
<% end %>
I know, there is no results presentation yet, it doesn't work at all at this moment (:
When i go to http://localhost:3000/exams/search it's mapping it to show controller and search is a :id paramether then...
How to get http://localhost:3000/exams/search to run the seach controller?
You forgot to add route. Put this in routes.rb, before resources :exams
map.search '/exams/search', :controller => :exams, :action => :search
Note, that resources :exams doesn't generate routes for all public methods of the controller, it generates very specific set of routes. You can find more information in the rails routing guide. (see section 3.2 in particular)
You'll need to add additional parameters to your mapping. You can add "collection" methods like so:
map.resources :exams, :collection => {:search => :get}
When you rake routes, you'll see that it generates something like so:
search_exams GET /exams/search(.:format) {:controller=>"exams", :action=>"search"}
exams GET /exams(.:format) {:controller=>"exams", :action=>"index"}
POST /exams(.:format) {:controller=>"exams", :action=>"create"}
new_exam GET /exams/new(.:format) {:controller=>"exams", :action=>"new"}
edit_exam GET /exams/:id/edit(.:format) {:controller=>"exams", :action=>"edit"}
exam GET /exams/:id(.:format) {:controller=>"exams", :action=>"show"}
PUT /exams/:id(.:format) {:controller=>"exams", :action=>"update"}
DELETE /exams/:id(.:format) {:controller=>"exams", :action=>"destroy"}

Resources