#224 Controllers in Rails 3
Jul 26, 2010 | 10 minutes | Controllers, Rails 3.0
Embedded flash notices, permanent cookies, and the details of respond_with are in this episode.
- source codeProject Files in Zip (95.2 KB)
- mp4Full Size H.264 Video (19.7 MB)
- m4vSmaller H.264 Video (12.4 MB)
- webmFull Size VP8 Video (30.9 MB)
- ogvFull Size Theora Video (28.7 MB)
Thanks for the casts Ryan. They are always appreciated.
Brilliant Ryan, thank you very much!
Have you also looked into José Valim's work on Inherited Resourses. I'd love to see you do something on that theme also.
Keep up the top work!!!
Thanks a lot for the screecasts.
It seems a really small error has gone through, in the destroy method of your controller, the respond_with(@product) shall produce an error I think (as the product has just been destroyed).
That aside, you really rock!
respond_with accepts :only and :except options as well.. :)
you can use it for specific actions.. that's the only thing I missed on the video
great work man
@Renato, did you mean the respond_to method that takes these options?
I was wondering what the best way to test these controllers is when testing what happens depending whether the record is valid or not.
My best try so far:
errors = ActiveModel::Errors.new(Post.new)
errors.add_on_blank(:id) Post.any_instance.stubs(:errors).returns(errors) Post.any_instance.stubs(:valid?).returns(false)
Speaking of respond_with - What do you think about putting it in an after_filter to avoid duplication?
Bryan, for respond_with you removed the HTML specific part - but you left the flash in there? So what happens when an XML request is served? The flash is set but it's useless for XML, so it is delivered with the next HTML request, to which it does not belong? Or what am I missing?
One thing that drives me nuts about the flash is that is sticks around until the next page is loaded.
For example, I like having a single/format for my flash messages and I use them for confirmation messages when CRUD'ing.
However if a user fails to save a record then the page is not redirected but a render event is called instead. This means that the flash is nicely displayed on the current error page but it ALSO sticks around for the next page (Where it isnt correct). I keep having to manually delete the flash whenever an action fails (after the render call) so that it isn't carried on to the next page.
I had no clue you could do 'redirect_to [:edit, @object]'. This is so much more concise than writing out 'redirect_to edit_object_path(@object)' in my opinion, especially when it comes to nested routes. Great episode :)
@RJ You want to use flash.now instead of flash. Michael Hartl's Rails Tutorial at www.railstutorial.org (free to read online for rails 2.3 and 3, free to download for rails 2.3) talks about it. hope that helps.
When an object has errors, it will redirect to the location if specified. It is not handled in this screencast, but it is not working as it should.
Does anyone have a copy of this gist? It is now deleted. I am running into the exact problem that Matthijs is talking about where a failed validation is changing the url.
@RJ Micheal Hart's book is great, but if you're in a hurry I found this concise blog post: http://www.rubysavedmylife.com/2007/12/15/flashnotice-vs-flashnownotice/
Basic gist is use flash redirects and flash[:now] for renders.
I was trying to use the new respond_with way of handling the restful responses in various formats, but I always was getting the same html thing....
The problem was that:
I had in the first place named the view template index.rhtml instead of index.html.erb.
Without the proper naming all the times the template was rendered not just for html.
Any way, thanks for the great screencast.
Thanks! I'm assuming that when we add :js as an argument to respond_to, respond with will render @object.to_json if no js.erb template is found?
respond_with is so sweet. It's odd that the generators with rails 3 still generate respond_to dos.
yes you cn get more on [How hack roblox android]
Très bon post!