$ ls public/
404.html 500.html index.html
422.html favicon.ico robots.txt
test.log:
Started GET “/static_pages/home” for 127.0.0.1 at 2012-08-31 10:04:28
-0700
Processing by StaticPagesController#home as HTML
Rendered static_pages/home.html.erb within layouts/application (3.7ms)
Completed 200 OK in 21ms (Views: 20.0ms | ActiveRecord: 0.0ms)
[1m[35m (0.1ms)[0m rollback transaction
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
Started GET “/static_pages/home” for 127.0.0.1 at 2012-08-31 10:04:28
-0700
Processing by StaticPagesController#home as HTML
Completed 200 OK in 2ms (Views: 1.6ms | ActiveRecord: 0.0ms)
[1m[35m (0.1ms)[0m rollback transaction
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
Yet here is my controller:
class StaticPagesController < ApplicationController
end
Previously, I had actions in my controller, but I wanted to see if the
tests would fail without the actions, and I fully expected them to fail,
but they won’t. I have everything at github:
Curiously, if I write a test for a non-existent view, I get three
errors: First, no route; then after I add the route:
get ‘/static_pages/something’
I get an error saying there’s no action; then after I add the action, I
get an error saying there’s no view; then after adding a view, the test
passes. Subsequently, if I delete the action for that view, the test
still passes. Why?
$ rake routes
static_pages_home GET /static_pages/home(.:format)
static_pages#home
static_pages_help GET /static_pages/help(.:format)
static_pages#help
static_pages_about GET /static_pages/about(.:format)
static_pages#about
static_pages_contact GET /static_pages/contact(.:format)
static_pages#contact
~/rails_projects/sample_app25$
And here is my routes.rb:
SampleApp25::Application.routes.draw do
get “static_pages/home”
get “static_pages/help”
get “static_pages/about”
get “static_pages/contact”
The priority is based upon order of creation:
first created -> highest priority.
Sample of regular route:
match ‘products/:id’ => ‘catalog#view’
Keep in mind you can assign values other than :controller and
:action
Sample of named route:
match ‘products/:id/purchase’ => ‘catalog#purchase’, :as =>
:purchase
This route can be invoked with purchase_url(:id => product.id)
Sample resource route (maps HTTP verbs to controller actions
automatically):
resources :products
Sample resource route with options:
resources :products do
member do
get ‘short’
post ‘toggle’
end
collection do
get ‘sold’
end
end
Sample resource route with sub-resources:
resources :products do
resources :comments, :sales
resource :seller
end
Sample resource route with more complex sub-resources
resources :products do
resources :comments
resources :sales do
get ‘recent’, :on => :collection
end
end
Sample resource route within a namespace:
namespace :admin do
# Directs /admin/products/* to Admin::ProductsController
# (app/controllers/admin/products_controller.rb)
resources :products
end
You can have the root of your site routed with “root”
just remember to delete public/index.html.
root :to => ‘welcome#index’
See how all your routes lay out with “rake routes”
This is a legacy wild controller route that’s not recommended for
RESTful applications.
Note: This route will make all actions in every controller
errors: First, no route; then after I add the route:
get ‘/static_pages/something’
I get an error saying there’s no action; then after I add the action, I
get an error saying there’s no view; then after adding a view, the test
passes. Subsequently, if I delete the action for that view, the test
still passes. Why?
What’s in your routes.rb? This could be a catch-all route firing. In
Ryan B.’ Railscast on semi-static pages, he mentions that a
completely empty controller will work as long as there is a properly
named view file at the expected path. I tried it in a quick test app,
but it didn’t work in vanilla 3.2.
SampleApp25::Application.routes.draw do
get “static_pages/home”
get “static_pages/help”
get “static_pages/about”
get “static_pages/contact”
Yup. I can confirm that with a similar route here in an otherwise
entirely empty controller and a matching view in the correct folder,
this just works without any logic at all. Let’s hear it for convention
over configuration!
It’s a feature of Rails. It’s a problem if it causes problems. With the
sort of routing you have, it’s only going to expose the views you
indicate in your routes.rb file. If you had a more liberal route like
match ‘info/:action’ => ‘info#action’
then it would show any action.html you placed in the views/info folder,
as long as it was requested properly.
On Saturday, September 1, 2012 11:05:36 PM UTC+1, Ruby-Forum.com User
wrote:
You can skip the adding an action bit and go straight to creating a view
But if I then delete the dog action, and then use the same url, http://localhost:3000/static_pages/dog, in my browser, this time I get a
different result–instead of getting an error the view displays. For
me, that inconsistent behavior is not right, and it should not be that
way. So I want to know whether there were reasons for the rails team
to institute that inconsistent behavior, or whether it is a bug.
This is a long standing rails feature (right back to 1.x at least): rails
doesn’t make you define an empty action. I don’t think it’s a feature
that
gets a whole lot of use, but it’s definitely intentional (documented
here: Layouts and Rendering in Rails — Ruby on Rails Guides
)
I don’t like this ‘default rendering’, nor the misleading error message
saying the action is missing–but I guess I’ll have to live with it.
According to the docs, a
route and view is all that’s necessary to render a page.
browser, rails complains that there’s no action called dog in the
But if I then delete the dog action, and then use the same url, http://localhost:3000/static_pages/dog, in my browser, this time I get a
different result–instead of getting an error the view displays. For
me, that inconsistent behavior is not right, and it should not be that
way. So I want to know whether there were reasons for the rails team
to institute that inconsistent behavior, or whether it is a bug.
It is not inconsistent. You started with no action and no view and
url fails. You added action and view and url works. You deleted only
the action and the view continues to work. If you delete the view
then it will fail again. Perfectly consistent. In fact the empty
action is just not needed. Rails assumes that since you have provided
a route and a view that you would like them to work and saves you the
effort of providing an empty action. I never intentionally rely on
that myself as I find it a little confusing, but it is not
inconsistent.
It’s a feature of Rails. It’s a problem if it causes problems.
(Note: nothing in this post has anything to do with rspec.)
For me, that routing behavior is causing problems. Here’s why: if I add
a route such as:
get “static_pages/dog”
…and then enter the url http://localhost:3000/static_pages/dog in my
browser, rails complains that there’s no action called dog in the
StaticPagesController:
===
Unknown action
The action ‘dog’ could not be found for StaticPagesController
Then if I add the dog action to the controller, then create a view,
everything works fine and dandy.
But if I then delete the dog action, and then use the same url, http://localhost:3000/static_pages/dog, in my browser, this time I get a
different result–instead of getting an error the view displays. As far
as I am concerned, that inconsistent behavior is not right.
So I want to know whether there were reasons for the
rails team to institute that inconsistent behavior, or whether it is a
bug.
As far as I know, the most basic concept of rails is that urls get
routed to controllers and actions. So what’s going on?
Okay, perhaps it’s more correct to say that Rails is flat out wrong.
Rails complains that I need an action when there isn’t one, which is
patently false. Is it asking too much to ask Rails not to lie to me (a
poor beginner)?