There’s a part of me that doesn’t like to use render :update in my methods, as I like to keep all of that view stuff, well, with the view stuff. But at the same time, I sometimes only need to update one thing on a page. Do I really need an entire file for this? And now that Chris showed me how to access view helpers in my controllers, that really alleviates the need for one-liner RJS templates. Which do you prefer?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | ## OPTION 1 ## Thoughts: nice to have it all there, but is sort of inconsistent with MVC, ## which could cause confusion for other developers looking for RJS def display_name @person = Person.find(params[:id]) render :update do |page| page.replace_html 'dom-id', @person.full_name end end ## OPTION 2 ## Thoughts: consistent with MVC, but it's hard for me to have one line in RJS def display_name @person = Person.find(params[:id]) end # display_name.rjs page.replace_html 'dom-id', @person.full_name |
Personally, I do both. Is that right? I often find it difficult to answer the question, “Why didn’t you use RJS there?” I end up saying, well, because it’s only one line. But I’ve also used render :update with two lines, and three lines. And on the contrary, I’ve used RJS with one, two, or three lines. I guess I’m just looking for a balance.
I’d like to be consistent and just use one or the other. It strikes me as odd anytime I open a RJS file and see one (or so) line(s). Which is the best practice? Which do you prefer?








01
Chris on Tue Mar 20 at 07:49AM
I tend to start off with a render update when I’m testing an action, and then I end up moving it to RJS. However, lately I go straight to the RJS because I know that eventually I may need to update that RJS with some super fancy effects and delays :-p Seriously, though, I’d say just make that RJS file – one line in an RJS file is less than three lines in the controller, and it’s cleaner with respect to MVC.
02
Simon on Tue Mar 20 at 08:25AM
The second one: always make the RJS. That way you’ll never have to think about where that update code lives. There’s nothing wrong with one-line files – they’re easy to read, at least! :)
Also, if you’re working in a team, it keeps things simple and predictable.
03
Nick on Tue Mar 20 at 08:54AM
Ooh – what a surprise – Ruby code I’ve never seen before! :-p I (quite obviously) have no real practical opinion, but I would just say the first option only because I hate having to look in multiple files to change one little thing. If you only need a one-liner, keep it in the controller. Just my opinion.
04
Ryan on Wed Mar 21 at 05:09AM
I always do
render :updateas a way to quickly test, too. But I was getting a little lazy, and beginning to question if it was necessary to move it to RJS. But definitely, 1 line in RJS is much better than 3 lines in the controller. I just needed to reinforce my thinking, I suppose.