I have the following routes in Refinery:
$ rake routes | grep blog
blog_root /blog(.:format) refinery/blog/posts#index
blog_post GET /blog/posts/:id(.:format) refinery/blog/posts#show
...
However, when I try to access that route, it gives an error.
> app.refinery.blog_post_path
ActionController::RoutingError: No route matches {:action=>"show", :controller=>"refinery/blog/posts"}
Here is part of routes.rb
# Refinery
mount Refinery::Core::Engine, at: '/'
Rails 3.2.14, Refinery 2.1.1.
Two things spring to mind
You probably don't need to preface refinery.blog_post_path with app although that doesn't seem to be the source of your error.
More importantly, you have not specified which blog post you want to show. So, your code should look something like refinery.blog_post_path my_blog_post.id
Related
I’m using Rails 5. I have this in my rake routes
localhost:sims nataliab$ rake routes
Prefix Verb URI Pattern Controller#Action
scenario_confidential_memos GET /scenarios/:scenario_id/confidential_memos(.:format) confidential_memos#index
POST /scenarios/:scenario_id/confidential_memos(.:format) confidential_memos#create
new_scenario_confidential_memo GET /scenarios/:scenario_id/confidential_memos/new(.:format) confidential_memos#new
edit_scenario_confidential_memo GET /scenarios/:scenario_id/confidential_memos/:id/edit(.:format) confidential_memos#edit
scenario_confidential_memo GET /scenarios/:scenario_id/confidential_memos/:id(.:format) confidential_memos#show
PATCH /scenarios/:scenario_id/confidential_memos/:id(.:format) confidential_memos#update
PUT /scenarios/:scenario_id/confidential_memos/:id(.:format) confidential_memos#update
DELETE /scenarios/:scenario_id/confidential_memos/:id(.:format) confidential_memos#destroy
scenarios GET /scenarios(.:format) scenarios#index
POST /scenarios(.:format) scenarios#create
new_scenario GET /scenarios/new(.:format) scenarios#new
edit_scenario GET /scenarios/:id/edit(.:format) scenarios#edit
scenario GET /scenarios/:id(.:format) scenarios#show
PATCH /scenarios/:id(.:format) scenarios#update
PUT /scenarios/:id(.:format) scenarios#update
DELETE /scenarios/:id(.:format) scenarios#destroy
Yet when I start my rails development server and visit http://localhost:3000/sims/scenarios/new , I get the error
Routing Error
No route matches [GET] "/sims/scenarios/new"
Why do I get this error when my path is in my rake routes?
The prefix sims is not present on the url I guess you meant. It would be http://localhost:3000/scenarios/new, ain't it?
Do you want to organize your scenarios under a sims namespace ? if so, check out routing in this guide:
http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
In essence, you'd want to have something like:
namespace :sims do
resources :scenarios do
resources :confidential_memos
end
end
So in our Rails 4.2 application, there is the alchemy_cms gem which requires its routes to be mounted last in config/routes.rb.
SampleApp::Application.routes.draw do
#other routes for the rails app here
# :
# :
mount Alchemy::Engine => '/'
end
We get routes like "/somehacker/routingurl" which then falls out to the Alchemy::Engine to handle, resulting in a default 500 error. If I wanted to do a custom 404 error, then the proper way to do it is to build a custom 404 page and have Alchemy handle the redirect? Otherwise since the Alchemy docs specify that it has to be the last route in config/routes.rb, there won't be a way to add a catchall route to redirect to some kind of error page I assume.
EDIT:
One of the problems is that there are some routes that are like the invalid "somehacker" route above that do need to be parsed by the Alchemy routing engine, such as "/en/us" where "en" is a valid locale. This is why I initially thought to put the route handling in the Alchemy engine's routes file.
If it is difficult for you to configure and use the Alchemy cms gem to redirect unknown routes into a custom defined page, you can use the bellow method to implement the same with a small coding tweak given bellow:
Rails 4.XXX
1. First Method.
(routes.rb)
You can still use a simple get to redirect all unknown routes.
get '*path', to: 'home#index'
If you wish to provide routing to both POST and GET requests you can still use match, but Rails wants you to specify the request method via via.
match "*path" => "home#index", via: [:get, :post]
Remember that routes.rb is executed sequentially (matching the first route that fits the supplied path structure), so put wildcard catching at the bottom of your matchings.
Here you can replace the home#index with any custom path that you defined in you application, also note that it is important to keep this redirection code only at the bottom of routes.rb.
2. You can follow the bellow tutorial on the same problem in a different perspective to solve can be found.
Custom 404 error page with Rails 4
I implemented a simple custom errors solution.
this one: http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages
everyhing is working fine except the missing routes in the routes.rb file..
in order to get to my error_controller when there is a missing route i did the wildcard solution: match '*not_found', to: 'errors#error_404'
but... now when i try to enter a sub section of my site which seats under:
/admin, i get to the error page. the wilcard gets triggered, even tough the route for admin section is defined in a different route file, under: config/routes/admin.rb
what can I do?
thanks
edit:
using rails 3.0.20 and ruby 1.8.7
If you're using Rails 3.2+, there is a simpler solution for your routes. First in 'config/application.rb' set your app as the error handler
config.exceptions_app = self.routes
Now when there is an your app will look to your routes to handle it. In 'config/routes.rb' you can add a route such as:
match "/404", :to => "errors#not_found"
A more verbose explanation can be found here.
OK so until I will update to Rails 3.2+
I simply put '*not_found', to: 'errors#error_404' into the last route file that is loaded.
that way its truly in the end of the routes and now all my routes work. and the error is still fired when needed.
On visiting localhost:3000 I'm getting the typical:
No route matches [GET] "/"
I don't want to see that error screen. I already trying with localhost:3000/passbook/v1/passes/ but still nothing so I typed:
rake routes
and got this output:
passbook GET /passbook/v1/passes/:pass_type_identifier/:serial_number(.:format) passbook/passes#show {:pass_type_identifier=>/([\w\d]\.?)+/}
GET /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier(.:format) passbook/registrations#index {:pass_type_identifier=>/([\w\d]\.?)+/}
POST /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#create {:pass_type_identifier=>/([\w\d]\.?)+/}
DELETE /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#destroy {:pass_type_identifier=>/([\w\d]\.?)+/}
passbook_log POST /passbook/v1/log(.:format) passbook/logs#create
How can I resolve this?
You haven't told Rails yet what your root path is. You can do this by saying
root to: "passes#show"
in config/routes.rb. Afterwards, you should see this in the rake routes output:
root / passes#show
Then it should work!
Additional tip
This may be over the top for your question, but following the test driven approach you should write a spec first:
describe "custom routing" do
it "shows the root page" do
get("/").should route_to("passes#show")
end
end
Use RSpec for this. Run the test in your console before having written the route itself by doing $ rspec and see the test fail at the right place.
Then, implement the routes above, run the test again—it should work. This way you ensure that you have written just enough code to meet your requirements, and that it is really the code you have written that lets you access the root path.
Is this a rails app you inherited from someone else?
Given the rake routes output you have provided, a url like localhost:3000/passbook/v1/passes/<EXAMPLE_PASS_TYPE_IDENTIFIER>/<EXAMPLE_SERIAL_NUMBER> should work based on the rule in the first line of the output, provided you replace EXAMPLE_PASS_TYPE_IDENTIFIER and EXAMPLE_SERIAL_NUMBER with valid values from the database.
The error message No route matches [GET] "/ for localhost:3000 is because there is no mapping for root in rake routes output.
You could modify the config/routes.rb file and add the following line at the bottom of the file:
root :to => "<controller_name>#<action_name>"
Eg:
root :to => "passbook/index"
if there is a passbooks_contoller.rb with a index method.
Best to learn more about rails routing from the documentation. Even better go through the Rails Tutorial to get a good understanding of the rails framework.
In this rails app:
https://github.com/ryanb/govsgo/blob/master/app/views/authentications/index.html.erb
There is a form that as 'new_user_path'
Where is the definition for this symbol? Is it a symbol??
Just confused as I downloaded the source, and searched for 'new_user' and couldn't find any reference to it.
It is located in routes.rb with the name new_user. It's not in this file.
EDIT:
routes.rb is here: https://github.com/ryanb/govsgo/blob/master/config/routes.rb.
But this is rails 3 format and I don't see new_user route. Run rake routes and you should see one called new_user.
I haven't downloaded the codebase but if you run rake routes you will probably see a route definition for new_user.
If you check config/routes.rb you will see the line resources :users. This creates several paths (like edit_user_path, users_path etc.), amongst them new_user_path.
You can see all generated path by running $ rake routes.