codyc4321
codyc4321

Reputation: 9672

what happens behind the scenes of the 7 default ruby on rails routes?

on codeacademy I see 7 default rails routes like new, show, index, etc...

But when you declare these routes in the controller it seems you still enter the info by hand.

Like here, it wants me to name a @tag out...normally you feed it the model and it just knows:

class TagsController < ApplicationController
    def index
        @tags = Tag.all
    end

    def show 
    @tag = Tag.find(params[:id]) 
    @destinations = @tag.destinations 
    end
end

Do naming your controller functions within the 7 default routes automate things I can't see? So if I renamed 'show' 'showsomestuff' but left the rest the same, does it work differently?

If not, what is the purpose of naming this routes as such if it doesn't automate anything? Is it just convention?

I'm used to django classbased views hiding a lot of cruft after feeding it a model or object ID, thanks

Upvotes: 0

Views: 71

Answers (3)

Elvn
Elvn

Reputation: 3057

Convention over configuration

There isn't really any such thing as "default rails routes." You could have any, all or none of the actions you listed.

To your question: "If not, what is the purpose of naming this routes as such if it doesn't automate anything? Is it just convention?"

Yes. It is convention. Convention over configuration is a core tenet of Ruby on Rails.

Ruby on Rails® is an open-source web framework that’s optimized for programmer happiness and sustainable productivity. It lets you write beautiful code by favoring convention over configuration.

You'll find little automation in RoR, but a lot of convention, which makes order of the underlying complexity. Naming conventions are critical, which is where you see the "index", "show" etc. action names. They aren't defaults, and in fact could be "indyx", "bob", etc. but that would violate convention over configuration.

I recommend, if you're getting started in RoR that you grab hold of the intro spec and really grasp it. The model, controller and view naming conventions are doctrine. You could rebel, but your code will be unmaintainable and you may become an outcast (just kidding about the last part) but it is important.

Upvotes: 0

Borsunho
Borsunho

Reputation: 1157

Naming does nothing by itself. Rails normally uses a callback:

before_action :set_tag, only: [:show, :edit, :update, :destroy]

...

def set_tag
  @tag = Tag.find(params[:id])
end

You can actually see this added to your generated code.

Upvotes: 0

Gavin Miller
Gavin Miller

Reputation: 43825

In my experience, and I've worked on some large rails apps, this ends up being a good thing. As you grow, what gets inserted into the @tags variable in your example gets more complicated, such as:

def index
  @tags.for(current_user).most_popular(10) # ... etc
end

If you're looking to eliminate boilerplate code, your best bet is to use generators, which will automatically fill out your controllers:

rails generate controller Tags

Upvotes: 1

Related Questions