views:

36

answers:

1

I appreciate this is an incredibly noob question, but having Googled multiple combinations of search terms I regretfully am still in the dark. These things can be difficult when one doesn't know and so obvious when one does.

I have semi-home page where incoming customers can choose to see a queried list or do a handful of other things. This isn't a home page but a sort of mini 'switchboard' within the site.

The seven standard RESTful Rails controller methods are (as I understand them):

List # shows a list of records generated with .find(:all)
Show # shows details on one record
New # initiates new record
Create # saves and renders/redirects
Edit # finds and opens existing record for edit
Update # updates attributes
Delete # deletes record
  1. What to use when some users need to see a selected 'list' of records that isn't literally .find(:all)? How would this work given I still need a list function that gives me .find(:all) for other purposes?
  2. I've heard of 'index' being used in Rails controllers, but I don't know the difference between index and list.
  3. For best practice and best design, what controller methods would you use for a mini-switchboard (and other intermediate pages such as 'About Us')?

Any specific answers would be a bit more useful than links to http://guides.rubyonrails.org/action_controller_overview.html etc. :) Thanks very much.

+2  A: 

First, I think it's important to note that the "standard methods" are neither standard nor methods in a sense. These are considered actions, and are only standard in that they are the conventions used with scaffolding. You can create any number of actions and group them logically with a controller.

If you open up [Project]/config/routes.rb and read through the comments, I think you'll understand a little better how controllers and actions map to a specific route. For instance, you can create a named route to the login controller's login action and call it authenticate by adding to the top of your routes.rb:

# ex: http://localhost/authenticate
map.authenticate 'authenticate', :controller => 'login', :action => 'login'
# map.[route] '[pattern]', :controller => '[controller]', :action => '[action]'

#  ex: http://localhost/category/1
map.category 'category/:id', :controller => 'categories', :action => 'list'

# ex: http://localhost/product_group/electronics
map.search 'product_group/:search', :controller => 'products', :action => 'list'

To partially answer your question, you may want to consider adding a category model and associating all products to a category. then, you can add a named route to view items by category as in the code block above.

The major benefit to using named routes is that you can call them in your views as category_url or category_path. Most people don't want to do this and rely on the default route mappings (at the end of the routes.rb):

 # ex: http://localhost/category/view/1
 map.connect ':controller/:action/:id'

 # ex: http://localhost/category/view/1.xml
 # ex: http://localhost/category/view/1.json
 map.connect ':controller/:action/:id.:format'

The key thing to mention here is that when a URI matches a route, the parameter that matches against a symbol (:id, or :search) is passed into the params hash. For instance, the search named route above would match a search term into params[:search], so if your products have a string column called 'type' that you plan to search against, your products controller might look like:

class Products < ApplicationController
  def list
    search_term = params[:search]
    @products = Product.find(:all, :conditions => ["type = ?", search_term])
  end
end

Then, the view [Project]/app/views/products/list.html.erb will have direct access to @products

If you'd really like an in-depth view into Ruby on Rails (one that is probably 10 times easier to follow than the guide in the link that you posted) you should check out
Agile Web Development with Rails: Second Edition, 2nd Edition

Jim Schubert
Thank you very much for this detailed reply Jim. I really do appreciate the time. Part of my query is simply the distinction between the term 'list' and the term 'index'. How are these two used differently?
sscirrus
The difference between the words is a matter of preference. 'Index' is borrowed from file-based web servers where, for example, 'index.html' is the starting point when accessing a directory. You could easily call this controller action 'main', 'start' or anything else. I personally only use an index action in the controller that I map as the root for the rails application. Then, I try to make sure all controller actions are verbs after that. My reason is that most apps will have one entry point, then everything else fires because of a user action.
Jim Schubert
Thank you very much.
sscirrus