How to change routes in ruby on rails?

Moon picture Moon · Oct 6, 2011 · Viewed 19.5k times · Source

I just installed Ruby on Rails and created a scaffold called posts. RoR generated controllers and other required files for me.

I created a new method in posts_controller, but I can't access it. I looked at other methods that are in the controller and looks like I need to access them by /posts/[MY POST ID]/[MY METHOD NAME].

Assuming I created my custom method hello in the controller, how do i access it?

I looked at routes.rb, but there's no configuration for it.

Updated:

I understand that I can manually configure it in routes.rb, but how do all the other methods work? For example, I have "edit", and "update" methods in the "posts_controller.rb" controller. How do those two methods work without configuring routes?

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
  end

I can't find a configuration that matches /posts/[0-9]/edit pattern.

Answer

Jordan Running picture Jordan Running · Oct 6, 2011

The documentation you're looking for is Rails Routing From the Outside In. Once you've read this you'll understand everything Rails does to take your request and point it at method in your controller.