Active Controller, Part 2


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

Action Methods:

  • controller looks for something in the following order when it processes a request:
    1. public instance method with the same name as the incoming action – invokes that method if found
    2. controller implements method_missing – invokes it with parameters: 1) action name, 2) empty argument list
    3. template named after the current controller and action – renders the template directly
    4. UnknownAction error is generated
  • prevent methods from being accessible as actions by marking them as private, or by using hide_action (but this is bad style):
    • hide_action :add_member
Controllers: lots of useful methods, including action_name, params and more:
Responding to the User: controller normally returns a set of HTTP headers, which can include no output (generally used for Ajax request responses)
  • only one response to the user per request
  • just before the controller finishes handling a request, it checks to see if a response has been generated and if not, looks for a template for that controller and action – if it finds one, the controller renders it
  • in render, Rails assumes that the extension is .html.erb unless specified otherwise

Rendering templates: template for action action of controller control will be in the file app/views/control/ (type is one of htmlatom and js, xxx is one of erb, builder, or rjs). Calling render doesn’t terminate the processing of an action. Ways of calling render: (it takes a hash of options)

  • no parameters: will simply render the view for the action (but you don’t even need to define the function for this to happen)
  • :text => string
    • renders the given string literally
  • :inline => string, [: type => “erb”|”builder”|”rjs”], [:locals => hash]
    • the inline hash value is interpreted as the source of a template of the given type – sort of like you’re on the fly creating a template file with that text
    • the contents of the locals hash are used to set the local variables in the template
  • :action => action_name
    • renders the given action’s template (should most likely use redirects instead) – does not call the action method, just renders the template
  • :template => name, [:locals => hash]
    • to render the template app/views/results/view_marks: call with a template value of “results/view_marks”
  • :partial => name
    • next chapter
  • :nothing => true
    • renders nothing – so that the action’s template won’t be rendered perhaps?
  • :update – renders the block as an RJS template, passing in the object between | and |
    • render(:update) do |object| … end
  • using the method render_to_string returns what would be sent to the browser instead of actually sending it to the browser

Redirects: used to pass the processing of a request off to some other action

  • particularly useful after performing a transaction of some sort and then you want to display a message to the user and display all of the transactions – so use flash[:note] to set the message and then do redirect_to(:action => ‘display’)

Cookies: hash-like functionality, e.g. cookies[:my_cookie]

Flash: communicating between requests – these notices last until the end of the following request

  • hash
  • common use: pass error and informational strings from one action to the next
  • common keys: notice, error, warning

Filters: help to wrap the processing performed by actions

  • before/after: before_filter or after_filter :method_name, [:except => :avoid_method], [:only => :only_method]
    • the class’s method method_name will be called before or after the action
  • around_filter methods use yield to allow the action to do its processing

GET vs. POST requests:

  • use GET to retrieve information from the server
  • use POST to request a change of state on the server
  • use forms instead of links to perform actions that change the state on the server

No Responses Yet to “Active Controller, Part 2”

  1. Leave a Comment

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: