You are viewing an old revision of this post, from July 12, 2011 @ 21:24:40. See below for differences between this version and the current revision.
I wrote a program to help me memorize stuff a few years, partly for reviewing Japanese vocabulary (and grammar) and partly to give me an excuse to learn Rails. It’s done very well in the former regard; it was somewhat useful in the latter regard, but it’s simple enough (and used by few enough people) that it certainly hasn’t turned me into a Rails expert.
Rails is a moving target, and in particular the release of Rails 3.0 last year brought a fair amount of changes. I didn’t upgrade immediately, partly because I was busy with other things and partly because authlogic took a while to be upgraded to work with Rails 3.0. Eventually, though, authlogic got fixed, and Rails 3.1 release candidates started to appear, so I figured I should upgrade sooner rather than later or I’d be setting myself up for even more pain in the future.
As always, there was a very helpful Railscast. So with the help of it and the rails_upgrade plugin, I created a branch and started working at it.
Getting the basics working wasn’t too hard, so after a couple of hours I had something that passed all of my automated tests without any deprecation warnings. Unfortunately, when I fired it up in a browser, I found that the code to show the answer to a question didn’t work. I wasn’t completely shocked to see that, because I was aware that the move to unobtrusive JavaScript was one of the big changes in Rails 3; I knew some of the things I had to do (make sure that I included rails.js
and the csrf_meta_tag
), but the exact details of how to handle replacing part of a web page with code from the back end wasn’t entirely clear to me, and I didn’t find a web page that gave a completely braindead cookbook.
Having said that, it didn’t take too long to figure out an answer. And, in case it helps anybody else, here’s what I did: I started with a section of my view that looked like this:
<% form_remote_tag :url => { :action => :answer, :item => ask }, :update => "answer", :html => { :id => "show-answer" } do %> <%= submit_tag "Answer" %> <% end %>
and ended up with this:
<%= form_tag url_for(:action => :answer, :item => ask), :remote => true, :id => "show-answer" do %> <%= submit_tag "Answer" %> <% end %>
And in the controller method I removed the render :partial
line at the end, and added a view answer.js.erb
containing the following:
$('answer').update("<%= escape_javascript(render :partial => 'answer', :object => @item) %>");
Which is quite straightforward, and certainly the generated HTML is nice. The one thing that I don’t like about it is that I don’t know how to do a good job of testing what the controller method returns: when the controller method rendered HTML, I could use assert_select
, but that gets harder when it renders JavaScript.
The next issue ws that the keyboard shortcut for pressing that button didn’t work. This had actually been an issue when I originally wrote that code, and the method I’d used then (calling the onclick
action) no longer worked. The solution I came up with was to modify rails.js
to make handleRemote
public, and to call it directly, but I’m not thrilled with that, because there’s the danger that the code paths for clicking and for using the keyboard shortcut could diverge. Eventually I’ll probably switch over to using jQuery instead of Prototype, maybe $(...).click()
will do the right thing there?
At that point, I pushed the code live, quickly realized that the display for newlines was broken, and reverted back. I’d already converted to using the new automatic HTML escaping mechanisms when they hit Rails 2.x; unfortunately, they changed in Rails 3.0.8 (and stayed changed in Rails 3.0.9), in a way that meant that the result of concatenating html_safe
strings was no longer html_safe
. I solved it by sticking an html_safe
at the end of the entire block, but I don’t like that because it makes it too easy to accidentally introduce HTML-escaping bugs; maybe I’ll file a bug report on the issue.
After that, everything worked, and from start to finish it probably took around five or six hours in total (spread over a week or so), which is a quite reasonable amount of time for a major upgrade. And it feels to me like the code is faster after the upgrade, which was a pleasant bonus! Also, it got me chatting with a friend of mine who also uses memory, resulting in my improving the layout on his Android phone (to match the way it looks on iPhones); always nice to have an excuse to improve software like that. And I’m glad I got this done before Rails 3.1 appeared.
But I also wish I’d been paying a bit more attention to Rails’s evolution, so I would have been a bit more familiar with what I was in for. Honestly, I still feel a little bit conflicted about this—I don’t currently plan to become a Rails expert, and I expect I’ll give Lift a try the next time I want to write a web app—but I like Ruby and Rails enough that I don’t want to lose touch with them. (And I really do depend on this memory program, so I very much want to keep it working!) For now, I’ve subscribed to the Ruby on Rails: Talk mailing list; the signal-to-noise ratio there is, unfortunately, a lot worse than I’d like, but there’s enough signal that I’m sticking with it for the time being. And I’ll try to find time to watch a higher percentage of Railscasts than I had been doing.
Post Revisions:
- July 23, 2011 @ 18:44:37 [Current Revision] by David Carlton
- July 12, 2011 @ 21:24:40 by David Carlton
Changes:
July 12, 2011 @ 21:24:40 | Current Revision | ||
---|---|---|---|
Content | |||
Unchanged: I wrote a <a href="http:// malvasiabianca.org/archives/ 2009/09/multiuser- memorization/">program to help me memorize stuff</a> a few years, partly for reviewing Japanese vocabulary (and grammar) and partly to give me an excuse to learn Rails. It's done very well in the former regard; it was somewhat useful in the latter regard, but it's simple enough (and used by few enough people) that it certainly hasn't turned me into a Rails expert. | Unchanged: I wrote a <a href="http:// malvasiabianca.org/archives/ 2009/09/multiuser- memorization/">program to help me memorize stuff</a> a few years, partly for reviewing Japanese vocabulary (and grammar) and partly to give me an excuse to learn Rails. It's done very well in the former regard; it was somewhat useful in the latter regard, but it's simple enough (and used by few enough people) that it certainly hasn't turned me into a Rails expert. | ||
Unchanged: Rails is a moving target, and in particular the release of Rails 3.0 last year brought a fair amount of changes. I didn't upgrade immediately, partly because I was busy with other things and partly because <a href="https:/ /github.com/binarylogic/authlogic" >authlogic</a> took a while to be upgraded to work with Rails 3.0. Eventually, though, authlogic got fixed, and Rails 3.1 release candidates started to appear, so I figured I should upgrade sooner rather than later or I'd be setting myself up for even more pain in the future. | Unchanged: Rails is a moving target, and in particular the release of Rails 3.0 last year brought a fair amount of changes. I didn't upgrade immediately, partly because I was busy with other things and partly because <a href="https:/ /github.com/binarylogic/authlogic" >authlogic</a> took a while to be upgraded to work with Rails 3.0. Eventually, though, authlogic got fixed, and Rails 3.1 release candidates started to appear, so I figured I should upgrade sooner rather than later or I'd be setting myself up for even more pain in the future. | ||
Unchanged: As always, there was a very helpful <a href="http:// railscasts.com/ episodes/225- upgrading-to- rails-3-part- 1">Railscast</a>. So with the help of it and the <a href="https:/ /github.com/rails/ rails_upgrade" >rails_upgrade plugin</a>, I created a branch and started working at it. | Unchanged: As always, there was a very helpful <a href="http:// railscasts.com/ episodes/225- upgrading-to- rails-3-part- 1">Railscast</a>. So with the help of it and the <a href="https:/ /github.com/rails/ rails_upgrade" >rails_upgrade plugin</a>, I created a branch and started working at it. | ||
Unchanged: Getting the basics working wasn't too hard, so after a couple of hours I had something that passed all of my automated tests without any deprecation warnings. Unfortunately, when I fired it up in a browser, I found that the code to show the answer to a question didn't work. I wasn't completely shocked to see that, because I was aware that the move to unobtrusive JavaScript was one of the big changes in Rails 3; I knew some of the things I had to do (make sure that I included <code>rails.js</code> and the <code>csrf_meta_ tag</code>), but the exact details of how to handle replacing part of a web page with code from the back end wasn't entirely clear to me, and I didn't find a web page that gave a completely braindead cookbook. | Unchanged: Getting the basics working wasn't too hard, so after a couple of hours I had something that passed all of my automated tests without any deprecation warnings. Unfortunately, when I fired it up in a browser, I found that the code to show the answer to a question didn't work. I wasn't completely shocked to see that, because I was aware that the move to unobtrusive JavaScript was one of the big changes in Rails 3; I knew some of the things I had to do (make sure that I included <code>rails.js</code> and the <code>csrf_meta_ tag</code>), but the exact details of how to handle replacing part of a web page with code from the back end wasn't entirely clear to me, and I didn't find a web page that gave a completely braindead cookbook. | ||
Unchanged: Having said that, it didn't take <em>too</em> long to figure out an answer. And, in case it helps anybody else, here's what I did: I started with a section of my view that looked like this: | Unchanged: Having said that, it didn't take <em>too</em> long to figure out an answer. And, in case it helps anybody else, here's what I did: I started with a section of my view that looked like this: | ||
Unchanged: <code><pre> | Unchanged: <code><pre> | ||
Unchanged: <% form_remote_tag :url => { :action => :answer, :item => ask }, | Unchanged: <% form_remote_tag :url => { :action => :answer, :item => ask }, | ||
Unchanged: :update => "answer", :html => { :id => "show-answer" } do %> | Unchanged: :update => "answer", :html => { :id => "show-answer" } do %> | ||
Unchanged: <%= submit_tag "Answer" %> | Unchanged: <%= submit_tag "Answer" %> | ||
Unchanged: <% end %> | Unchanged: <% end %> | ||
Unchanged: </pre></code> | Unchanged: </pre></code> | ||
Unchanged: and ended up with this: | Unchanged: and ended up with this: | ||
Unchanged: <code><pre> | Unchanged: <code><pre> | ||
Unchanged: <%= form_tag url_for(:action => :answer, :item => ask), | Unchanged: <%= form_tag url_for(:action => :answer, :item => ask), | ||
Unchanged: :remote => true, :id => "show-answer" do %> | Unchanged: :remote => true, :id => "show-answer" do %> | ||
Unchanged: <%= submit_tag "Answer" %> | Unchanged: <%= submit_tag "Answer" %> | ||
Unchanged: <% end %> | Unchanged: <% end %> | ||
Unchanged: </pre></code> | Unchanged: </pre></code> | ||
Unchanged: And in the controller method I removed the <code>render :partial</code> line at the end, and added a view <code>answer.js.erb</code> containing the following: | Unchanged: And in the controller method I removed the <code>render :partial</code> line at the end, and added a view <code>answer.js.erb</code> containing the following: | ||
Unchanged: <code><pre> | Unchanged: <code><pre> | ||
Unchanged: $('answer').update("<%= escape_javascript(render :partial => | Unchanged: $('answer').update("<%= escape_javascript(render :partial => | ||
Unchanged: 'answer', :object => @item) %>"); | Unchanged: 'answer', :object => @item) %>"); | ||
Unchanged: </pre></code> | Unchanged: </pre></code> | ||
Deleted: Which is quite straightforward, and certainly the generated HTML is nice. The one thing that I don't like about it is that I don't know how to do a good job of testing what the controller method returns: when the controller method rendered HTML, I could use <code>assert_ select</code>, but that gets harder when it renders JavaScript. | Added: Which is quite straightforward, and certainly the generated HTML is nice. The one thing that I don't like about it is that I don't know how to do a good job of testing what the controller method returns: when the controller method rendered HTML, I could use <code>assert_ select</code>, but that gets harder when it renders JavaScript. (<strong>Update:</strong> <a href="http:// malvasiabianca.org/archives/ 2011/07/testing- updates-via- unobtrusive-javascript/">I figured out a good way to test this.</a>) | ||
Unchanged: The next issue ws that the keyboard shortcut for pressing that button didn't work. This had actually been an issue when I originally wrote that code, and the method I'd used then (calling the <code>onclick</code> action) no longer worked. The solution I came up with was to modify <code>rails.js</code> to make <code>handleRemote</code> public, and to call it directly, but I'm not thrilled with that, because there's the danger that the code paths for clicking and for using the keyboard shortcut could diverge. Eventually I'll probably switch over to using jQuery instead of Prototype, maybe <code>$(...).click()</code> will do the right thing there? | Unchanged: The next issue ws that the keyboard shortcut for pressing that button didn't work. This had actually been an issue when I originally wrote that code, and the method I'd used then (calling the <code>onclick</code> action) no longer worked. The solution I came up with was to modify <code>rails.js</code> to make <code>handleRemote</code> public, and to call it directly, but I'm not thrilled with that, because there's the danger that the code paths for clicking and for using the keyboard shortcut could diverge. Eventually I'll probably switch over to using jQuery instead of Prototype, maybe <code>$(...).click()</code> will do the right thing there? | ||
Unchanged: At that point, I pushed the code live, quickly realized that the display for newlines was broken, and reverted back. I'd already converted to using the new automatic HTML escaping mechanisms when they hit Rails 2.x; unfortunately, they changed in Rails 3.0.8 (and stayed changed in Rails 3.0.9), in a way that meant that the result of concatenating <code>html_safe</code> strings was no longer <code>html_safe</code>. I solved it by sticking an <code>html_safe</code> at the end of the entire block, but I don't like that because it makes it too easy to accidentally introduce HTML-escaping bugs; maybe I'll file a bug report on the issue. | Unchanged: At that point, I pushed the code live, quickly realized that the display for newlines was broken, and reverted back. I'd already converted to using the new automatic HTML escaping mechanisms when they hit Rails 2.x; unfortunately, they changed in Rails 3.0.8 (and stayed changed in Rails 3.0.9), in a way that meant that the result of concatenating <code>html_safe</code> strings was no longer <code>html_safe</code>. I solved it by sticking an <code>html_safe</code> at the end of the entire block, but I don't like that because it makes it too easy to accidentally introduce HTML-escaping bugs; maybe I'll file a bug report on the issue. | ||
Unchanged: After that, everything worked, and from start to finish it probably took around five or six hours in total (spread over a week or so), which is a quite reasonable amount of time for a major upgrade. And it feels to me like the code is faster after the upgrade, which was a pleasant bonus! Also, it got me chatting with a friend of mine who also uses memory, resulting in my improving the layout on his Android phone (to match the way it looks on iPhones); always nice to have an excuse to improve software like that. And I'm glad I got this done before Rails 3.1 appeared. | Unchanged: After that, everything worked, and from start to finish it probably took around five or six hours in total (spread over a week or so), which is a quite reasonable amount of time for a major upgrade. And it feels to me like the code is faster after the upgrade, which was a pleasant bonus! Also, it got me chatting with a friend of mine who also uses memory, resulting in my improving the layout on his Android phone (to match the way it looks on iPhones); always nice to have an excuse to improve software like that. And I'm glad I got this done before Rails 3.1 appeared. | ||
Unchanged: But I also wish I'd been paying a bit more attention to Rails's evolution, so I would have been a bit more familiar with what I was in for. Honestly, I still feel a little bit conflicted about this—I don't currently plan to become a Rails expert, and I expect I'll give <a href="http:// liftweb.net/">Lift</a> a try the next time I want to write a web app—but I like Ruby and Rails enough that I don't want to lose touch with them. (And I really do depend on this memory program, so I very much want to keep it working!) For now, I've subscribed to the <a href="http:// groups.google.com/ group/rubyonrails- talk?pli=1">Ruby on Rails: Talk</a> mailing list; the signal-to-noise ratio there is, unfortunately, a lot worse than I'd like, but there's enough signal that I'm sticking with it for the time being. And I'll try to find time to watch a higher percentage of Railscasts than I had been doing. | Unchanged: But I also wish I'd been paying a bit more attention to Rails's evolution, so I would have been a bit more familiar with what I was in for. Honestly, I still feel a little bit conflicted about this—I don't currently plan to become a Rails expert, and I expect I'll give <a href="http:// liftweb.net/">Lift</a> a try the next time I want to write a web app—but I like Ruby and Rails enough that I don't want to lose touch with them. (And I really do depend on this memory program, so I very much want to keep it working!) For now, I've subscribed to the <a href="http:// groups.google.com/ group/rubyonrails- talk?pli=1">Ruby on Rails: Talk</a> mailing list; the signal-to-noise ratio there is, unfortunately, a lot worse than I'd like, but there's enough signal that I'm sticking with it for the time being. And I'll try to find time to watch a higher percentage of Railscasts than I had been doing. |
Note: Spaces may be added to comparison text to allow better line wrapping.
[…] talking about upgrading memory to Rails 3.0, I mentioned that I’d had trouble testing a bit of the unobtrusive JavaScript code. […]
7/23/2011 @ 6:43 pm