dsp_099
dsp_099

Reputation: 6121

Rails: simple routing question

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 :)

Upvotes: 0

Views: 71

Answers (1)

tadman
tadman

Reputation: 211540

The resources and its singular variant resource routing 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 routes listing. Each of these could be specified manually with a series of match statements, but generally that's not a very effective way to do it.

The reason for using resources is to encourage conforming to standard REST conventions.

Upvotes: 4

Related Questions