<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Sławosz Sławiński</title>
 
 <updated>2011-11-11T12:30:51-08:00</updated>
 <id>http://slawosz.github.com/</id>
 <author>
   <name>Sławosz Sławiński</name>
   <email>slawosz@gmail.com</email>
 </author>

 
 <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/slawosz" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="slawosz" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
   <title>How do I test? Spork and Emacs experience.</title>
   <link href="http://slawosz.github.com/2011/08/11/how-do-i-test-spork-and-emacs" />
   <published>2011-08-11T00:00:00-07:00</published>
   <updated>2011-08-11T00:00:00-07:00</updated>
   <id>http://slawosz.github.com/2011/08/11/how-do-i-test-spork-and-emacs</id>
   <content type="html">Few weeks ago &lt;a href="https://github.com/ryanb"&gt;Ryan Bates&lt;/a&gt; created a &lt;a href="http://railscasts.com/episodes/275-how-i-test"&gt;screencast&lt;/a&gt; about testing. He uses &lt;a href="https://github.com/guard/guard"&gt;Guard&lt;/a&gt; to run specs. Guard watches changes in files and may run appropriate specs files, which is very convenient. However, I have a little different way to run specs.
&lt;p&gt;I do not always like to run full spec file, very often I need only to run one spec. To accomplish it very easily, I use few functions in my emacs, thanks to &lt;span class="strike"&gt;&lt;a href="https://github.com/eschulte/rinari"&gt;rinari&lt;/a&gt; mode&lt;/span&gt; &lt;a href="https://github.com/remvee/emacs-rails"&gt;emacs-rails&lt;/a&gt; mode:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;rails-spec:run-this-spec&lt;/code&gt; &amp;#8211; runs specs with current line as rspec line parameters. I use this command very very often (&lt;code&gt;C-c C-c ,&lt;/code&gt; shortcut)&lt;/p&gt;
&lt;p&gt;&lt;code&gt;rails-spec:run-current&lt;/code&gt; &amp;#8211; runs all spec in current spec file or specs for file I actually edit. It works exactly like Guard, but I use it only when I need to (&lt;code&gt;C-c C-c z .&lt;/code&gt;shortcut)&lt;/p&gt;
&lt;p&gt;&lt;code&gt;rails-spec:run-last&lt;/code&gt; &amp;#8211; this function runs lately ran spec. It is great for &lt;span class="caps"&gt;TDD&lt;/span&gt;/&lt;span class="caps"&gt;BDD&lt;/span&gt; workflow, because with this I can run only one spec while I work on implementation in corresponding file (&lt;code&gt;C-c C-c z l&lt;/code&gt; shortcut)&lt;/p&gt;
&lt;p&gt;One of the most important things in &lt;span class="caps"&gt;TDD&lt;/span&gt;/&lt;span class="caps"&gt;BDD&lt;/span&gt; process is running specs after each change in code. Thus we run spec even hundreds times in (good) developer day. That is why specs should run very fast. We may say, that specs that run more than 10 seconds are distracting  and may cause checking lastest rss/facebook/google+ news ;) Of course, I talk about single spec or single spec file, because it is normal that running entire test suite takes few minutes in large projects. But we run entire test suite before commit, or even we use CI.&lt;/p&gt;
&lt;h2&gt;Spork&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://github.com/timcharper/spork"&gt;Spork&lt;/a&gt; is a great tool for &lt;span class="caps"&gt;TDD&lt;/span&gt;/&lt;span class="caps"&gt;BDD&lt;/span&gt;. It is test server, it loads our app to memory and forks for spec you actually run. With this, spec runs immediately, without waiting for rails app bootstraping. It makes a big difference in specs speed. For example I did some benchmarks in Teambox project:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;All spec file, without spork:
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;time&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rspec spec/models/user_spec.rb&lt;span class="o"&gt;)&lt;/span&gt;

...............................................................................

Finished in 22.38 seconds
79 examples, 0 failures

real	0m35.714s
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Single spec:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;time&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rspec spec/models/user_spec.rb -l 90&lt;span class="o"&gt;)&lt;/span&gt;

Run filtered using &lt;span class="o"&gt;{&lt;/span&gt;:line_number&lt;span class="o"&gt;=&lt;/span&gt;&amp;gt;90&lt;span class="o"&gt;}&lt;/span&gt;
.

Finished in 0.54218 seconds
1 example, 0 failures

real	0m14.283s
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And with Spork:&lt;br /&gt;
Whole spec file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;time&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rspec spec/models/user_spec.rb --drb&lt;span class="o"&gt;)&lt;/span&gt;
...............................................................................

Finished in 24.55 seconds
79 examples, 0 failures

real	0m27.399s
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And single spec with spork:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;time&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;rspec spec/models/user_spec.rb -l 90 --drb&lt;span class="o"&gt;)&lt;/span&gt;
Run filtered using &lt;span class="o"&gt;{&lt;/span&gt;:line_number&lt;span class="o"&gt;=&lt;/span&gt;&amp;gt;90&lt;span class="o"&gt;}&lt;/span&gt;
.

Finished in 0.51661 seconds
1 example, 0 failures

real	0m3.286s
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Nice speed up!  With spork, single spec lasts about 3 seconds. In other projects with longer bootstrap time, we save much more time!&lt;/p&gt;
&lt;p&gt;To run rspec with spork, first run spork &lt;code&gt;`spork`&lt;/code&gt; and then run any rspec with &lt;code&gt;`--drb`&lt;/code&gt; option. Do not forget to add this option to spec runner in your editor. Before first run we have to bootstrap spork with command &lt;code&gt;`spork --bootstrap`&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;My test flow:&lt;/h2&gt;
&lt;p&gt;1 I write spec&lt;br /&gt;
2 Run only this spec &lt;code&gt;(rails-spec:run-this-spec&lt;/code&gt; in emacs)&lt;br /&gt;
3 It does not pass.&lt;br /&gt;
4 I write the code to make test pass&lt;br /&gt;
5 I run last spec (&lt;code&gt;rails-spec:run-last&lt;/code&gt; in emacs)&lt;br /&gt;
6 If spec does not pass, back to point 4&lt;br /&gt;
7 Spec pass&lt;br /&gt;
8 Run  spec for entire file (&lt;code&gt;rails-spec:run-current&lt;/code&gt;) or even for the whole project.&lt;/p&gt;
&lt;p&gt;I am hope that this note will usefull for You. I encourage you to run&lt;br /&gt;
your spec with spork and run them with your editor. It gives you much&lt;br /&gt;
more fun in &lt;span class="caps"&gt;TDD&lt;/span&gt;/&lt;span class="caps"&gt;BDD&lt;/span&gt;!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Add new resource with ajax</title>
   <link href="http://slawosz.github.com/2011/04/26/add-new-resource-with-ajax" />
   <published>2011-04-26T00:00:00-07:00</published>
   <updated>2011-04-26T00:00:00-07:00</updated>
   <id>http://slawosz.github.com/2011/04/26/add-new-resource-with-ajax</id>
   <content type="html">&lt;p&gt;In &lt;a href="http://railscasts.com/episodes/258-token-fields"&gt;Railscast 258&lt;/a&gt; &lt;a href="https://github.com/ryanb"&gt;Ryan Bates&lt;/a&gt; shows how to use &lt;a href="http://loopj.com/jquery-tokeninput/"&gt;Token Fields&lt;/a&gt;, javascript plugin which helps adding entries for &lt;code&gt;many&lt;/code&gt; and &lt;code&gt;many to many&lt;/code&gt; association. In this article I will exetend railscast and show you how to create not existing entries with ajax.&lt;/p&gt;
&lt;p&gt;You can &lt;a href="https://github.com/slawosz/Add-resource-with-ajax-and-facebox-tutorial"&gt;download&lt;/a&gt; application for this tutorial from github or use it on &lt;a href="http://add-new-resource-with-ajax.heroku.com/"&gt;heroku&lt;/a&gt; to see how it works. I started with application created by Ryan in 258 episode.&lt;/p&gt;
&lt;p&gt;To add authors we can go to &lt;code&gt;/author/new&lt;/code&gt; url, but when we already on &lt;code&gt;/books/new&lt;/code&gt; we rather want to render form with ajax. To render form we will use &lt;a href="https://github.com/defunkt/facebox"&gt;facebox&lt;/a&gt;, writen in jquery by &lt;a href="https://github.com/defunkt"&gt;Chris Wanstrath&lt;/a&gt;. So, simply download facebox from github, decompress and place &lt;code&gt;facebox.css&lt;/code&gt; in &lt;code&gt;public/stylesheets&lt;/code&gt;,  &lt;code&gt;facebox.js&lt;/code&gt; in &lt;code&gt;public/javascripts&lt;/code&gt; and &lt;code&gt;loading.gif&lt;/code&gt; and &lt;code&gt;closelabel.png&lt;/code&gt; in &lt;code&gt;public/images&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Now, point write paths for images in &lt;code&gt;public/javascripts/facebox.js&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="js"&gt;&lt;span class="nx"&gt;loadingImage&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/images/loading.gif&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nx"&gt;closeImage&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/images/closelabel.png&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;and place stylesheet and javascript in our layout &lt;code&gt;app/views/layouts/application.html.erb&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="rhtml"&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;stylesheet_link_tag&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;application&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;token-input-facebook&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;facebox&amp;quot;&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;javascript_include_tag&lt;/span&gt; &lt;span class="ss"&gt;:defaults&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;jquery.tokeninput&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;facebox&amp;quot;&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now it is time to show form for new author resource on facebox.&lt;/p&gt;
&lt;p&gt;Lets add link in &lt;code&gt;app/views/books/_form.html.erb&lt;/code&gt; partial:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="rhtml"&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="ss"&gt;:author_tokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Authors&amp;quot;&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;link_to&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Add new author&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_author_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:remote&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;It looks like a normal link, but with one exception: it has a remote option, indicating, that we want perform an ajax request with this link. This is new way in Rails 3 to generate ajax request and I think it is great. It simply adds a html &lt;code&gt;data-remote='true'&lt;/code&gt; attribute to our link. Javascript code located in &lt;code&gt;public/javascripts/rails.js&lt;/code&gt; binds &lt;code&gt;onclick&lt;/code&gt; event on such link. Whe link is clicked, an ajax request is performed to links url, in our case to &lt;code&gt;new_author_path&lt;/code&gt;. So now, we have rewrite new method in &lt;code&gt;AuthorsController&lt;/code&gt;, which is our link destination. You can read more about Rails 3 unobtrusive javascript in &lt;a href="http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/"&gt;Simone Carletti&lt;/a&gt; blog.&lt;/p&gt;
&lt;p&gt;With Rails it is so simple :) We had to add only a new template in js format, &lt;code&gt;app/views/authors/new.js.erb&lt;/code&gt;. When we click link, method new will recognize that request is in js format and render js template. So now, in template we will write code, which will render new action on facebox:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="rhtml"&gt;$.facebox(&amp;#39;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;escape_javascript&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:template&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;books/new.html&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&amp;#39;);
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Code above generate a form on facebox with &lt;code&gt;app/views/books/new.html.erb&lt;/code&gt; template. We have to add write new.html to generate html. Much more often in rich ajax Rails application we use partials, but I would like to show how to achieve our goal with minimal effort.&lt;/p&gt;
&lt;p&gt;If you would like to know more about using facebox, see &lt;code&gt;public/javascripts/facebox.js&lt;/code&gt;. Every use case is described in this file.&lt;/p&gt;
&lt;p&gt;Now, we want to send our form and create a new author.&lt;br /&gt;
Normaly, we can use&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="ss"&gt;:remote&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;option in our form, but we only use remote form in facebox. So, let&amp;#8217;s add data-remote attribute to ajax form in facebox. To do it, we will add following to &lt;code&gt;app/views/books/new.js.erb&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="rhtml"&gt;$(&amp;#39;#facebox form&amp;#39;).data(&amp;#39;remote&amp;#39;,&amp;#39;true&amp;#39;);
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
This will add attribute &lt;code&gt;data-remote='true'&lt;/code&gt; to our form, and with this attribute, &lt;code&gt;public/javascripts/rails.js&lt;/code&gt; will process our form with ajax. After submiting, the request goes to method create in &lt;code&gt;AuthorsController&lt;/code&gt; and  is looking for js template. In this template, we need only close facebok. So, we create file &lt;code&gt;app/views/authors/create.js.erb&lt;/code&gt; and write:

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="rhtml"&gt;$(document).trigger(&amp;#39;close.facebox&amp;#39;);
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Thus we would like to perform other actions in html and js request in this method, we use &lt;code&gt;respond_to&lt;/code&gt; block and redirect on html format and render template on js format:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
  &lt;span class="vi"&gt;@author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Author&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:author&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@author&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&lt;/span&gt;
    &lt;span class="n"&gt;respond_to&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="vi"&gt;@author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:notice&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Successfully created author.&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;js&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:action&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;new&amp;#39;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now it works, but it need some more improvements. When &lt;code&gt;@author&lt;/code&gt; instance cannot be save, we should render form again in facebox.  In &lt;code&gt;app/models/author.rb&lt;/code&gt; we add validadion:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:presence&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now we cannot add new no-name author. When we try, we got validation error, which we want to show in facebox.&lt;/p&gt;
&lt;p&gt;At first we need to change create action to use js format in else section:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
  &lt;span class="vi"&gt;@author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Author&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:author&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@author&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&lt;/span&gt;
    &lt;span class="n"&gt;respond_to&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="vi"&gt;@author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:notice&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Successfully created author.&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;js&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="n"&gt;respond_to&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:action&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;new&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;js&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;And finally change &lt;code&gt;app/views/books/create.js.erb&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="rhtml"&gt;&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@author&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
  $(document).trigger(&amp;#39;close.facebox&amp;#39;)
&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
  $(&amp;#39;#facebox .content&amp;#39;).html(&amp;#39;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;escape_javascript&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:template&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;authors/new&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&amp;#39;)
  $(&amp;#39;#facebox form&amp;#39;).data(&amp;#39;remote&amp;#39;,&amp;#39;true&amp;#39;);
&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now we have fully working form on facebox, with minimal changes in our code. We can do last small improvement, remove link &amp;#8216;Back to List&amp;#8217;. In facebox link is completely unnecessary. After checking source of &lt;code&gt;app/views/authors/new.html.erb&lt;/code&gt; we simply need remove last &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag. In jQuery, it&amp;#8217;s simple:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="js"&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;#facebox .content p:last&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We add this line at the end &lt;code&gt;app/views/authors/new.js.erb&lt;/code&gt; and at the end in else section in &lt;code&gt;app/views/authors/create.js.erb&lt;/code&gt;. Now we are done.&lt;/p&gt;
&lt;p&gt;See our application working on &lt;a href="http://add-new-resource-with-ajax.heroku.com/"&gt;heroku&lt;/a&gt;, and check the&lt;br /&gt;
repo on &lt;a href="https://github.com/slawosz/Add-resource-with-ajax-and-facebox-tutorial"&gt;github&lt;/a&gt;.&lt;br /&gt;
All changes you can see in &lt;a href="https://github.com/slawosz/Add-resource-with-ajax-and-facebox-tutorial/commit/cf13adacdff595059dc6eba0fcb33c0204ad6714"&gt;cf13adacdff595059dc6eba0fcb33c0204ad6714&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I hope this tutorial will be helpfull for You. Any feedback is welcome.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Run single test case to speed up development</title>
   <link href="http://slawosz.github.com/2010/09/29/run-single-test-case" />
   <published>2010-09-29T00:00:00-07:00</published>
   <updated>2010-09-29T00:00:00-07:00</updated>
   <id>http://slawosz.github.com/2010/09/29/run-single-test-case</id>
   <content type="html">&lt;p&gt;When you use common TestUnit (maybe with great gem &lt;a href="http://github.com/thoughtbot/shoulda"&gt;Shoulda&lt;/a&gt;) or &lt;a href="http://rspec.info/"&gt;RSpec&lt;/a&gt;, you may speed up development by running only single test case.&lt;br /&gt;
In TestUnit, run test file with `-n` switch:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;ruby -n &lt;span class="s2"&gt;&amp;quot;name_of_test_method&amp;quot;&lt;/span&gt; path/to/test_file.rb
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;or use regexp:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;ruby -n &lt;span class="s2"&gt;&amp;quot;/name of test method/&amp;quot;&lt;/span&gt; path/to/test_file.rb
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Regexp is very convinient when you work with shoulda.&lt;/p&gt;
&lt;p&gt;In Rspec, you can use `-e`:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;rspec -e &lt;span class="s1"&gt;&amp;#39;name of pattern&amp;#39;&lt;/span&gt; path/to/spec_file.rb
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Or maybe you would like to install &lt;a href="http://www.zenspider.com/ZSS/Products/ZenTest/"&gt;autotest&lt;/a&gt; or &lt;a href="http://github.com/dchelimsky/rspec/wiki/Autotest-Integration"&gt;autospec&lt;/a&gt; for Rspec?&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Business seconds - my first gem</title>
   <link href="http://slawosz.github.com/2010/08/16/business-seconds-my-first-gem" />
   <published>2010-08-16T00:00:00-07:00</published>
   <updated>2010-08-16T00:00:00-07:00</updated>
   <id>http://slawosz.github.com/2010/08/16/business-seconds-my-first-gem</id>
   <content type="html">&lt;p&gt;In June, I have released my first gem, &lt;a href="http://github.com/slawosz/business_seconds"&gt;business seconds&lt;/a&gt; It is very simple extension for &lt;a href="http://github.com/bokmann/business_time"&gt;business time&lt;/a&gt;&lt;br /&gt;
In my project I needed to count business time in seconds between two time instances. At first, i forked business time, and start implementing my feature, but this is on not todo list in its &lt;span class="caps"&gt;README&lt;/span&gt;. So, I wrote it in separate gem, which has business time in dependencies&lt;/p&gt;
&lt;p&gt;I wish my work will help some of You. Enjoy!&lt;/p&gt;</content>
 </entry>
 
 
</feed>

