Restful routes, not repeating yourself and non-standard rest

So I’ve recently started playing with the restful routes support in
edge rails, converting a few of my controllers to support all the
standard operations… One thing that’s been bothering me, however, is
the following:

Before using restul routes, I would define an edit method in my
controller which would be responsible for both creating new objects as
well as editing pre-existing objects (if, for example, an id was passed
as a parameter)… The form would postback to the same edit action in
the controller, which would then update the attributes of the object
and save them back to the database. This worked out pretty well, since
both the edit and new actions would use the same view, and I didn’t
have to duplicate code, since edit and new are so similar.

Now, when moving to restful routes, it looks like edit and new should
be completely different actions (create and update), but they still
have very similar behaviour. So now we’re forced to use two views
whereas before I could get away with one… I’ve managed to avoid
duplicating my views by using a partial for the actual form data, and
then rendering this partial from both my create an update actions…
However, I’m just wondering how everyone else is handling the code in
the controller. Are you keeping both update and create completely
separate, or writing another method that you call from both create and
update to perform the tasks common to both actions?

I really like the idea of restful routes, but I’m having a hard time
adjusting from my old habits, especially when it seems like I’ll be
writing more code than before, and duplicating things that used to be
easy to combine. I liked the fact that I used to be able to both edit
and create new objects from a single action, so it’s a bit strange for
me to have to separate these methods.

Also, another thing that has been concerning me is that often I’ll have
both an administrative view and a regular user view for an object…
For example, I may have a list of contests that are displayed publicly
to all users via the index action in the contests_controller, but I
also want to display another (condensed) index listing for
administrators inside an adminstrative section. How does everyone
normally handle this situation? Do you do define a non-standard
restful route such as the following:

map.resources :contests, :collection => { :list => :get}

or do you specify a regular route as follows:

map.with_options(:controller => ‘contests’) do | contest |
contest. contest_list ‘contests/list’, :action => ‘list’

Or do you simply use the same index view for both admin and regular
users, but add conditional statements in the view to display a
different listing if the user is an administrator? (this isn’t really
ideal, since I do want administrators to see the same index as regular
users, but when they’re in the admin section of the site, I want them
to see the administrative display of the contests).

I used to just define an extra action (such as list_contests) in my
admin_controller, which would display the admin view for the given
object. But it seems to make more sense to put all of these things
into the same controller, so for example, the contests_controller is
responsible for all access to contest objects.

What I’m trying to understand is how to best handle all the non CRUD
related actions that I sometimes have associated with a controller. Is
it best to use a mapped resource, or to use a standard route…

Thanks for any suggestions or advice,


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs