No route matches "#show" for "#new" - ruby-on-rails

I've come across a strange routing issue that I am not sure how to solve.
I have a route that looks like...
new_tenants_venue GET /tenants/venues/new(.:format) tenants/venues#new
but when I hit that route, "/tenants/venues/new", I get the error...
No route matches {:action=>"show", :controller=>"tenants/venues", ...
I don't see any similar routes in the list that would interfere, especially before that route. After it comes the #show route but that has an :id rather than "new"

So, I made a classic noob mistake and didn't check the partial I included in the view. On one of the link_to paths I had a singular/plural mistake in the the name. It seemed as if it was the route that was causing the issue. Damn rails error codes!

Related

Rails4 routing: Is it possible to use permalinks or parameters directly under root?

I am attempting
get ":site_name", to: 'public_site#index'
hoping that
get("/catsfrommars").should route_to("public_site#index")
Unfortunately I am getting the :site_name routed as an action:
No route matches {:controller=>"public_site", :action=>"/catsfrommars"}
I also tried
get "/:site_name", to: 'public_site#index'
get "(:site_name)", to: 'public_site#index'
And the :site_name parameter still gets routed as an action on the specified controller.
Any ideas on how to fix this and get :site_name as a parameter?
Edit: I am trying to do something similar to this: Rails Routing Question: Mapping Slugs/Permalinks Directly under Root? but with Rails4 syntax. Once I get it working, I'll use constraints to isolate the namespaced app routes.
The route works properly, the issue was with rSpec.
{get: "/catsfrommars"}.should route_to(controller: "public_site",
action: "index", site_name: "catsfrommars")
resolved it, and more specifically moving the get into a block.

Rails routing error when using resource but now when using GET

I am creating a simple suggestion box app (to learn Rails) and am getting the following Rails routing error when I go to "/suggestion-boxes" running on my local machine (localhost:3000)
"Routing Error
No route matches [GET] "/suggestion-boxes"
In my routes.rb file I have:
SuggestionBoxApp::Application.routes.draw do
resources :suggestion_boxes
end
This is what I get when I run rake routes:
suggestion-box-app$ rake routes
suggestion_boxes GET /suggestion_boxes(.:format) suggestion_boxes#index
POST /suggestion_boxes(.:format) suggestion_boxes#create
new_suggestion_box GET /suggestion_boxes/new(.:format) suggestion_boxes#new
edit_suggestion_box GET /suggestion_boxes/:id/edit(.:format) suggestion_boxes#edit
suggestion_box GET /suggestion_boxes/:id(.:format) suggestion_boxes#show
PUT /suggestion_boxes/:id(.:format) suggestion_boxes#update
DELETE /suggestion_boxes/:id(.:format) suggestion_boxes#destroy
However, if I modify my routes file to
SuggestionBoxApp::Application.routes.draw do
get "suggestion-boxes" => "suggestion_boxes#index"
end
Then the page "/suggestion-boxes" displays as per the index action in my SuggestionBoxesController.
I tried restarting my server but this had no impact. While I of course can go with using GET, this error makes no sense, and I would like understand what is causing it.
Any insights would be very much appreciated.
The error is that you are not renaming the REST route, instead the controller one.
Try declaring
resources :suggestion_boxes, :as => "suggestion-boxes"
in your config/routes.rb file.
Somewhere in your code you are calling to suggestion-boxes controller which does not exist. Your controller is suggestion_boxes, spelling. So where every you have "suggestion-boxes" you should replace with "suggestion_boxes". The code that you added create an alias that matches 'suggestion-boxes' to the index action of the suggestion_boxes controller so this resolves it if it is your desired affect. But simply fixing your spelling would resolve your problem. I usually use the second approach if I want the change the URL that user see. Have a look at the routing doc for a better understanding

How is form_for setting the action of the html form?

Following the rails guides' tutorial, I ran into an issue where I learnt about plural vs. singular naming and repercussions. Now that I know this going forward, I'm interested in learning if it's possible to work around this -- without having to rename my controller.
I've passed a new :url_path to form_for (account_path) and my 'rake routes' outputs an entry:
account GET /account/:id(.:format) account#show
I still get the following error when trying to access the page with the form:
Routing Error
No route matches {:action=>"show", :controller=>"account"}
My routes.rb has an entry for the plural resources directory (resources :account)... Understanding that the singular version works, but the controller still represents "many".
Any insight for a green RoR dev?
Assuming it's not a typo - you should write resource :account. Hope that helps.

Resources-Route with nested GET breaks Resource-Routes?

Running Rails 3.2.11, I have a regular controller "LbuController" which is basically an enhanced scaffold, enhanced by 4 additional GET methods used for AJAX requests.
resources :lbus do
get 'add_offering'
get 'remove_offering'
get 'add_offering_element'
get 'remove_offering_element'
end
running rake routes gives me the following routes
lbu_add_offering GET /lbus/:lbu_id/add_offering(.:format) lbus#add_offering
lbu_remove_offering GET /lbus/:lbu_id/remove_offering(.:format) lbus#remove_offering
lbu_add_offering_element GET /lbus/:lbu_id/add_offering_element(.:format) lbus#add_offering_element
lbu_remove_offering_element GET /lbus/:lbu_id/remove_offering_element(.:format) lbus#remove_offering_element
lbus GET /lbus(.:format) lbus#index
POST /lbus(.:format) lbus#create
new_lbu GET /lbus/new(.:format) lbus#new
edit_lbu GET /lbus/:id/edit(.:format) lbus#edit
lbu GET /lbus/:id(.:format) lbus#show
PUT /lbus/:id(.:format) lbus#update
DELETE /lbus/:id(.:format) lbus#destroy
which seem exactly to be what I intended.
But following a link to http://localhost:3000/lbus/new created with link_to "new", new_lbu_path gives me the following error:
No route matches {:action=>"add_offering", :lbu_id=>nil, :offering_id=>1, :controller=>"lbus"}
which makes absolutely no sense for me.
Anybody got any ideas what's happening here and what's going wrong?
Thanks in advance!
On the 'new' view you must have used a lbu_add_offering path.
That path seems to be incorrect. The error refers to that :action=> "add_offering".

Using Ruby on Rails link_to to link to controller action

I'd just started toying around with Ruby on Rails and had come across an issue with linking to another action in a controller from a particular view. I am almost certain it's an issue (or lack of code) in my routes.rb file, but I think I'm misunderstanding exactly how this file works & what I have to do. I've got a solution but pretty sure it's not the "best way" to do it.
I have one controller called home with two actions, index (which is the default) and newbill. Inside index.html.erb I have:
<h1>Home View</h1>
<%= link_to "new", :controller => "home", :action => "newbill" %>
However I was getting a routing error:
No route matches {:controller=>"home", :action=>"newbill"}
Doing rake routes gives me the following:
root / {:controller=>"home", :action=>"index"}
I then (following some Googling) added this code to routes.rb
match 'home/newbill' => 'home#newbill', :as => :newbill
And then in my index.html.erb I've got this:
<%= link_to "Name", newbill_path %>
And now this works as expected. My questions however are:
Why does this work? What exactly is going on behind the scenes?
Surely this is not the best way to do it? Adding another match 'home/newbill'... for every controller / action I want to link to seems a rubbish way of doing things.
I really like Ruby, but struggling a bit with this aspect of Rails...routing in general is messing up my head a bit I think!
Any help is much appreciated :D
Thanks,
Jack
I guess the first time your code didn't work because your home controller is defined as a resource.
If you define a controller as a resource in routes.rb file it will support only 7 standard methods (according to REST architecture):
index
new
create
show
edit
update
destroy
If you need any more custom routes you should add them manually, say in your case 'newbill', may go as:
resources :home do
collection do
get :newbill
end
end
But as per my understanding, your newbill method should go to bills controllers new, method not in the home controller.
You are right, Rails routes are little bit confusing (at least for me), but once you understand you can do lots of cool stuff.
Read here for the Rails official routes documentation:
http://guides.rubyonrails.org/routing.html.
You should check out the Rails Routing guide. A read through will help you understand what is going on behind the scenes.
This works becuase rails filters every request through the router looking for a match. This enables you to define custom routes such as domain.com/post when the path is actually blog#post. Prior to rails 3, a catch-all route was the last route in the routes file. This allowed you to define a controller and action and it would just work. I'm on my iPad and not near any projects, so I can't verify it, but I think that route is still there in rails 3.1, it just needs to be umcommented.

Resources