Action Controller: Requests

24Sep09

(Notes from Chapter 21 of “Agile Web Development with Rails, Third Edition”)

ActionController and ActionView are modules contained in Action Pack: http://api.rubyonrails.org/

Routing basics: form is controller/action/ID style

  • new instance is created of the controller
  • its process method is called
  • controller calls a method with the same name as the action or (method_missing if it can’t find one)
  • if action method returns sans rendering something (or there is no action method with the name), controller will try to render a template with the same name

Routing comprehensively:

  • config/routes.rb contains the routing information for the application
  • using ruby script/console, we can play with a RouteSet (in ActionController::Routing) object for our application to see how it would work:
    • method recognize_path shows how routing would parse a particular incoming path. For example:
      rs.recognize_path “/assignments/edit/2”
      would output:
      { :action => “edit”, :controller => “assignments”, :id => “2” }
    • method generate shows the URL routing that would be created for a set of parameters. For example:
      rs.generate :controller => :students
      would return “/students”

      • like using the method url_for and link_for within your application
    • class method use_controllers! allows you to override the set of controllers, using an array – [ ], but you need to re-load the definition of the routes to be able to use this, i.e. load “config/routes.rb”
  • Naming routes – instead of using map.connect to define a route, use any other name than connect in the routing definition, such as map.index or map.date
    • You can now generate a URL by using the method name_url instead of url_for
  • Controller naming – if you don’t pass a controller to url_for, then it uses the current one
  • link_to method can be used like this:
    <%= link_to(“Index”, index_url) %>
    where index_url references to a route named index

Rendering partials

  • format:
    • render :partial => “/groups/group”
  • This indicates that you are using a partial template to display each individual group and that that particular template is located in the directory app/views/groups

Okay, that’s the end of Chapter 21. I think I’ll leave the other two chapters to look at for tomorrow since they are also reasonably long, I’m tired, and I have a grammar test first thing in the morning!

Advertisements


No Responses Yet to “Action Controller: Requests”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: