This should be fairly easy. I’m following along with a fairly outdated video course as it seems like but I’d like to figure this out nonetheless:
I created a controller called “Say,” which in turn created a say_controller.rb. Inside there, I created a new method called ‘hello,’ so the inside of say_controller looks like this:
class SayController < ApplicationController
def hello
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @derps }
end
end
end
Then, I created a hello.html.erb under /app/view/say/ with some html in it. If you try to access it at localhost:3000/say/hello, there’s a routing error. So I added this to routes.rb:
match 'say/hello' => 'say#hello'
Here’s the question though – if you run rails generate scaffold Derp, then in routes you’ll see
resources:derps
and that’s the only thing that’s gonna be there. How does Rails know to route to it without a specific match command? ie I kinda see what’s happening here but I’d like to understand the theory.
More importantly, what do I need to rely on in the future when creating views and controllers by hand (will I even have to do that?) – is it standard procedure in Rails to add a line to routes.rb manually for each and every view/controller?
Merci 🙂
The
resourcesand its singular variantresourcerouting specifiers actually create a number of routes at the same time in the hopes of making it a lot easier to define how your application is presented URL-wise.You can see the generated routes in the
rake routeslisting. Each of these could be specified manually with a series ofmatchstatements, but generally that’s not a very effective way to do it.The reason for using
resourcesis to encourage conforming to standard REST conventions.