#230 Inherited Resources
Sep 06, 2010 | 9 minutes | Controllers, Plugins
Inherited Resource simplifies RESTful controllers by providing default actions which can be customized through inheritance.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
Cool! :o)
Thank you for your awesome work! You really rock!
Brilliant, thanks Ryan!
By-the-way, how did you color your Gemfile?
+1 Cool! Thanks!
docgecko, look at http://github.com/drnic/Bundler.tmbundle
I like this, tho i dont know if id use this. Is it an abstraction too far? a little further and it would start to become active scaffold without the views. i like to see whats happening even at the expense of a little extra code and repetition. each to their own tho. good video +1
Hey Ryan nice screencast, one note, you should be using the new interpolation way for internationalization files, instead of {{var}} %{var} should be used.
First I thought, why the hell would I use this? The new rails 3.0 controllers are already so much cleaner when you are using respond_with. But getting rid of all the actions and just adding exceptions where you need them just rocks. This is particularly time saving when dealing with users and only users can see and edit stuff they created, just throw in:
protected
def begin_of_association_chain
current_user
end
and your already set for all actions. Thank you for another great episode!
Love the work you do but this is very dangerous IMO. Its way to easy to have a destroy method when you never intended to have one. Also I've found IR and resource controller to scream N + 1 query. This works for a simple CRUD admin though.
I've just rarely have simple CRUD controllers. =)
I wrote something here about Resource Controller http://www.mailinglistarchive.com/html/spree-user@googlegroups.com/2010-05/msg00211.html
Thanks for all the work you put in.
@docgecko, I just made the Gemfile be interpreted as Ruby in TextMate.
@Paul, whether or not this takes the abstraction too far depends on your application. I don't recommend it for everything, but if you're building an administration interface with repetitive RESTful controllers I imagine it would clean things up a lot.
@Jorge, thanks! I'll fix the show notes.
@David, thanks for pointing out the dangers of accidentally leaving the destroy action available. If one is concerned about this I recommend always defining the "actions" at the top of the controller even if you are doing all 7.
In the end I only recommend abstracting out the controller actions if the repetition is there in the application. Not all apps work well for this.
Be careful with the scope with a proc. The proc method behaves differently in Ruby 1.8 and 1.9 and you shouldn't use it (to avoid confusion). It's not a big deal here, but still.
Furthermore, there is no need for "named scopes" like this anymore. IMHO it's much cleaner to just create a class method:
http://pastie.org/1141847
This works _exactly_ the same as defining the scope like you did in the screencast.
One more tip, concerning InheritedResources. If you always want to redirect to the index action after update or create, you can also create a class inbetween ApplicationController and your regular controller. A second -more specific- application controller if you will. I usually have a "Backend::ApplicationController" which acts as the application controller for the admin part of the site. Example: http://pastie.org/1141861
I have been thinking for a while that this should be implemented in Rails itself. Just makes too much sense.
Another cool episode. :)
I'm using inherited_resources for admin part in my application, where I used scaffolds before. Its a great replacement for scaffolding :)
+1 to what @iain said
The use of proc is not recommended. Instead, you should use lambda or Proc.new. lambda is recommended in most cases, IMHO.
I also agree with what @iain said about using #scope with a Proc. #scope is great but, if you need to pass variables, a class method is *much* cleaner.
Great screencast. Glad to see InheritedResources working with Rails3!
Thanks for this episode. I was looking around for a nice replacement for make_resourceful and this is great! :)
As always, nice screencast.
I personally wouldn't use these gems though. Sure, keeping things DRY is generally a good thing, but there are times when being explicit and transparent about exactly what is happening is worth the price of a small amount of repetition.
So, I'll gladly keep going with the standard RESTful controller, with its 7 skinny actions.
As usual, thanks for your awesome screencasts!
I wondered if there is a way to tell inherited_resources to respond_to :xml by default. I configured my controller with "respond_to :xml, :atom", but when i'm asking for my resource like domain.com/xmlapi/resource, it tries to respond to html - a bug?
Does anyone know if {resource_name} is always available in rails?
Im also using blogengine before and its really pretty cool.
Cash for inheritance is an option available to heirs who wish to sell inheritance assets bequeathed to them through a Will or trust. When assets are held in probate it can take several months or years before final distribution occurs. If heirs do not want to wait for estate settlement, they can assign inheritance rights to a funding source in exchange for a lump sum cash payment.
Deprecation notice and advice from the author:
"Since Rails 3 came out, I have no longer used Inherited Resources. I have found that the responders abstraction and custom Rails generators offer the perfect balance between hiding and showing too much logic. That said, I suggest developers to make use of the responders gem (at github.com/plataformatec/responders) and no longer use Inherited Resources."
I was wondering when someone was going to mention that, and how much weight should be given to the author's advice...
I was wondering when someone was going to mention that, and how much weight should be given to the author's advice...
There's a lot of interesting info here! I'm very grateful for the post! movers and packers hyderabad , is here to make your life simpler.
Homepage : packersmovershyderabadcity.in/ ,
Blog : Blog.packersmovershyderabadcity.in ,
Packers And Movers is recognized as a business manager providing wide-ranging and differentiate service appearance as well as Relocation Shifting, Logistics and Transportation, Facilities managing, strategy & Designing services.
Lovely Website, Maintain the fantastic work. Thank you so much!
We Provide Best Packers And Movers Gurgaon List for Get Free Best Quotes, Compare Charges,
Save Money And Time, Household Shifting Services @
packers-and-movers-gurgaon.in
Lovely Website, Maintain the fantastic work. Thank you so much!
We Provide Best Packers And Movers Gurgaon List for Get Free Best Quotes, Compare Charges,
Save Money And Time, Household Shifting Services @
packers-and-movers-gurgaon.in