Alpha Blue
2011-Jul-23 03:29 UTC
Creating a Universal Controller - Here''s an example for anyone.
I''m sure there are some solid ideas out there for designing universal
controllers, but I wanted to share my design which seems to work well.
I''ll post all the code on gist.github.com for reading.
Here is the code:
https://gist.github.com/1100923
=> example_controller.rb
I call a set_tables before filter to supply what tables are valid with
the controller. If you have 12 different tables/models that will use
the default actions of this controller, you can list the tables in the
@tables array.
The default index action is fairly simple as well. The action first
checks to see if the database table is part of the valid @tables array,
and if not, it redirects the user to the root of the site and posts a
simple notice. Otherwise, if valid, it will begin to assign the data to
the @data instance variable.
The @data instance variable structures how the model will be referenced
by taking the params (as an example let''s say you pass
''example_model''.
The structure of the line would look like this:
ExampleModel.list_data
The list_data method is built in your model.
Once the data is assigned, the index view just renders @partial which is
equal to the table_index parameter. So, in this example, it will look
for _example_model.html.erb. This allows you to setup partials to
represent the index action for each table.
Lastly, the routes.rb file will basically setup a default match for:
http://yourdomain.com/example/:table_index where :table_index becomes
the parameter passed to your controller.
So, in our example, the url will look like:
http://yourdomain.com/example/example_model
which you can assign to any link_to using
"default_table_path(''example_model'')"...
Pretty simple and fairly clean.
Enjoy.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.