Controllers, Views, and Dynamic Content
Controllers
Controller's main function is to control things. Two main parts of it:
The flow of the application
Gather data that it needs (from model and such)
All the View does is formats the things for the output. In order to access data that is gathered by the controller instalce variables (object - controller variables) are used. This variables are set in the controller.
Note: There is no way to go back from the View to the Controller to set something up. Everything must be preset in the Controller for the View to use.
Rendering Templates
How does the controller decides which template to use?
Rails checks if there is any Action that is associated with the request. And if not checks if there is a View for it. If so, even without an action being present rails renders the view.
Render Method
Each action of your controller by default will have a render method executed with the defaults implicitly, if nothing else is specified. That means that the view with the action_name.html.erb under the controller's name folder in the views directory will be rendered for you automatically.
There are a variety of ways to customize the behaviour of render. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.
Her are multiple ways to render a view:
render(:action => 'hello')
render(:template => 'demo/hello')
render('demo/hello')
render('hello')
It might be a little confusing, but render(:action => 'hello') doesn't actually calls hello action, what it does is it renders a default template for the hello action, without executing the action itself.
So it will be an equivalent for all the other render examples in the above list.
The last two were added in Rails 3 and are now considered a best practice to use.
More on Render (Rails docs)
Redirect
Redirect is part of the http protocol itself. What Rails does when you are use it's redirect_to method is it send a request back to the browser with the HTTP status code: HTTP/1.1 302 Found and send a Location that it wants the request to be send to, e.g.: Location: http://localhost:3000/demo/hello. So the browser initiates a new request to the new location (all of the processing from the action before the redirect_to was called is lost.)
View Templates
Dynamic Templates with embedded Ruby code (ERb).
hello.html.erb Template name: hello Process with: ERb Output format: HTML
You Embed Ruby code with special tags: <% code %> and <%= code %>. To output Ruby variable you use #{variable_name} inside a double quoted string. E.g.:
<% name = "Alexander" %> <%= "My name is #{name}" %>
Note: you can't use puts and gets in the ERb, those output to the console and not to the generated html. Instead you use <%= %>.
Instance Variables
This are used to pass the data pulled together by the controller in the corresponding view.
Instance variables are variables of an instance of a class (object) and since a Rails controller is simply a class, instance variables are instances of the controller.
Instance variables are marked with an @ sign, e.g.: @instance_variable_name.
Link_to Method
In Rails templates you can use regular <a href="demo/action">Link</a> html links. However it is much more common to use Rails method link_to for that. E.g.: <%= link_to(text, target) %>
Text is the text to be displayed as a link. And target is where the url is going. You can specify the regular "controller/action" as target, but note that it can take Ruby hashes as well.
The syntax to do that is: {:controller => 'demo', :action => 'index'}. You can simply pass the action part, if the url is within the same controller.
The cool thing about this is that routes file is being used to form your url, same way as for the requests coming in.
Additional URL Parameters
When we need to pass additional url parameters like in: http://localhost:3000/demo/hello/3?page=3&name=kevin, we can simply use the same hash notation as we can use with link_to. Everything after the :action will be treated as parameters, e.g.: {:controller => 'demo', :action => 'hello', :id => 1, :page => 3, :name => 'kevin'}.
Note: that since ID is set to have a different meaning in our routes file (match ':controller(/:action(/:id(.:format)))'), the way the it will be appended to the url is different (/#{id} instead if /?id=#{id}).
Another point to note here is that all the url parameters (GET) and POST parameters are send to the system as strings, so in order to do any manipulation with them as integers, you would ned to cast them to such (.to_i).
All the parameters (including controller and action names) are accessible in the Rails/Ruby code using the special params hash.
You can simply access the values of it by passing either a name of the parameter you are looking for as a symbol or a string, e.g.: params[:id] or params['id'].
Note: you can view the content of the params (same as of any other hash, array or other Ruby constract) by calling inspect() method on it, e.g.: params.inspect().
















