Rails 3 Controller: Difference between revisions

From Wiki
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 21: Line 21:
root :to => "articles#index"
root :to => "articles#index"
</source>
</source>
NOTE: You must also delete <code>/public/index.html</code> to make sure the web server doesn't bypass Rails.


=== RESTful Routes and Resources ===
=== RESTful Routes and Resources ===
If you add  
If you add  
<pre>
<source lang="ruby">
resources :articles
resources :articles
</pre>
</source>
to <code>config/routes.rb</code>, the following named routes are automatically created:
to <code>config/routes.rb</code>, the following named routes are automatically created:
<pre>
<pre>
Line 34: Line 35:
new_article_path => /articles/new
new_article_path => /articles/new
</pre>
</pre>
If you generate a scaffold, you will get all seven of the default actions for RESTful controllers:
index, show, new, edit, create, update, and destroy
==== Nested Resources ====
The Comment object depends on the Article object, so create a nested resource in <code>config/routes.rb</code>:
<source lang="ruby">
resources :articles do
    resources :comments
end
</source>
This creates the following named routes:
<pre>
article_comments_path
article_comment_path
edit_article_comment_path
new_article_comment_path
</pre>
=== Redirecting ===
Use an object as shorthand for redirection:
<source lang="ruby">
redirect_to(@article)
</source>
is a shortcut equivalent to
<source lang="ruby">
redirect_to(article_path(:id => @article.id))
</source>
== Controllers ==
=== Generate Controller ===
<pre>
rails generate controller ControllerName [actions] [options]
</pre>
Example:
<pre>
rails generate controller Users
</pre>
=== ApplicationController ===
Every controller is a subclass of <code>ApplicationController</code>.  Any methods or data in <code>ApplicationController</code> are therefore available to all other controllers.
=== Flash Hash ===
Useful for sending messages from controller to view.  Use this in controller:
<source lang="ruby">
flash[:rubber_ducky] = "You're the one!"
</source>
and make it visible in the view with
<source lang="ruby">
<%= flash[:rubber_ducky] %>
</source>
Two special shorthand cases: <code>notice</code> and <code>alert</code>:
This code in controller:
<source lang="ruby">
format.html { redirect_to(@article, :notice => 'Article was successfully created.') }
</source>
and this in view:
<source lang="ruby">
<p class="notice"><%= notice %></p>
</source>

Latest revision as of 22:20, 6 July 2011

Routes

defined in config/routes.rb:

match ':controller(/:action(/:id(.:format)))'  # default route, :id and :format may be accessed as parameters in the action
match '/teams/home' => 'teams#index'           # call index action of teams controller
match '/teams/search/:query' => 'teams#search' # sends :query parameter to search action in teams controller
match '/teams/search/:query' => 'teams#search', :as => 'search'  # named route, defines search_url and search_path methods
search_url  => http://example.com/teams/search
search_path => /teams/search

useful in something like

link_to "Search", search_path

root url

use something like this in config/routes.rb:

root :to => "articles#index"

NOTE: You must also delete /public/index.html to make sure the web server doesn't bypass Rails.

RESTful Routes and Resources

If you add

resources :articles

to config/routes.rb, the following named routes are automatically created:

article_path => /articles/:id  
articles_path => /articles  
edit_article_path => /articles/edit/:id  
new_article_path => /articles/new

If you generate a scaffold, you will get all seven of the default actions for RESTful controllers: index, show, new, edit, create, update, and destroy

Nested Resources

The Comment object depends on the Article object, so create a nested resource in config/routes.rb:

resources :articles do
    resources :comments 
end

This creates the following named routes:

article_comments_path
article_comment_path
edit_article_comment_path
new_article_comment_path

Redirecting

Use an object as shorthand for redirection:

redirect_to(@article)

is a shortcut equivalent to

redirect_to(article_path(:id => @article.id))

Controllers

Generate Controller

rails generate controller ControllerName [actions] [options]

Example:

rails generate controller Users

ApplicationController

Every controller is a subclass of ApplicationController. Any methods or data in ApplicationController are therefore available to all other controllers.

Flash Hash

Useful for sending messages from controller to view. Use this in controller:

flash[:rubber_ducky] = "You're the one!"

and make it visible in the view with

<%= flash[:rubber_ducky] %>

Two special shorthand cases: notice and alert: This code in controller:

format.html { redirect_to(@article, :notice => 'Article was successfully created.') }

and this in view:

<p class="notice"><%= notice %></p>