Resources-Route with nested GET breaks Resource-Routes? - ruby-on-rails

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".

Related

Ruby on Rails route with 2 parameters not routing correctly

I'm learning Rails and to do that am making a small workout tracking app. I have a number of routes for my ProgramsController:
get "/programs/:user_id", to: "programs#show_all"
get "/programs/:user_id/new", to: "programs#new"
get "/programs/:user_id/program/:program_id", to: "programs#show"
post "/programs/:user_id/create", to: "programs#create"
get "/programs/:user_id/edit/:program_id", to: "programs#edit"
patch "/programs/:user_id/edit/:program_id", to: "programs#update"
delete "/programs/:user_id/programs/:program_id", to: "programs#destroy"
When I navigate to /programs/1, it executes the show action and renders the show.html.erb view, while it should be executing the show_all action and rendering the show_all.html.erb view. This seems like incorrect behavior to me, but I am new to Rails, so I am assuming I am missing something about how routes are resolved. How do I fix this routing error?
edariedl hit the nose on the head. I had defined resources :programs at the top of my routes.rb file and had just gotten tunnel vision and not noticed it.

Rails POST throws GET error

I have a rails link that's using the POST method:
link_to "Run", forecast_run_path(#forecast), method: :post
to call a defined post route:
resources :forecasts do
post :run
end
The route appears in rake routes output as expected:
forecast_run POST /forecasts/:forecast_id/run(.:format) forecasts#run
And when I inspect the page, the expected post link appears in the page:
Run Forecast
In dev it works fine, and was good in production until sometime just a few days ago - I can't find any change that seems like it should have broken it. But when I click the link, I get the error ActionController::RoutingError (No route matches [GET] "/forecasts/20/run")
I agree that no GET route matches, but it should be using POST.
This is Ruby 2.1.5, Rails 4.2.0
To my own embarrassment and for the benefit of anyone else who has the same issue:
As I switched among different clients, I didn't notice that on one of them NoScript in Firefox was blocking javascript on that site. Doh!
When you post a run, the form will direct you to the show action where it displays the newly posted run, in your case you don't have a show action for run, so implement a show action and should work fine.

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

No route matches "#show" for "#new"

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!

Beginner RoR Error After Generating Views (RailsSpace)

I'm using RailsSpace to learn Ruby on Rails and am coming across an error after performing what seems like a simple command.
I used the Terminal to generate a new User controller with the views Index and Register:
$ rails generate controller User index register
And it had no problem with that, creating the files index.html.erb and register.html.erb as well as all the other expected files.
But when I visit http://localhost:3000/user/register, it comes back with the error message:
ROUTING ERROR: No route matches {:controller=>"user",
:action=>"about"}
My routes.rb doesn't indicate any abnormalities:
RailsSpace::Application.routes.draw do
get "user/index"
get "user/register"
get "site/index"
get "site/about"
get "site/help"
root :to => "site#index"
end
Why does it try to route to the "About" action, and what other file can I edit to change this routing?
I'm using Rails 3 in case that matters.
I would try hand coding in the route. In your case it would look like this:
match '/user/register' => 'users#register', :as => :register
This will definitely work and prevent the page /user/register from going to the about page. Let me know how things go and Ill try to continue to steer you in the right direction.

Resources