Rails 3 Controller: Difference between revisions

From Wiki
Jump to navigation Jump to search
Created page with '== Routes == defined in <code>config/routes.rb</code>: <pre> match ':controller(/:action(/:id(.:format)))' # default route, :id and :format may be accessed as parameters in the …'
 
Line 7: Line 7:
match '/teams/search/:query' => 'teams#search', :as => 'search'  # named route, defines search_url and search_path methods
match '/teams/search/:query' => 'teams#search', :as => 'search'  # named route, defines search_url and search_path methods
</pre>
</pre>
<code>search_url</code> gives <code>http://example.com/teams/search</code>,
<pre>
<code>search_path</code> gives <code>/teams/search</code>
search_url gives http://example.com/teams/search
search_path gives /teams/search
</pre>
useful in something like
useful in something like
<source lang="ruby">
<source lang="ruby">
link_to "Search", search_path
link_to "Search", search_path
</source>
</source>

Revision as of 21:26, 30 June 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 gives http://example.com/teams/search
search_path gives /teams/search

useful in something like

link_to "Search", search_path