<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fuel Your Coding &#187; Ruby on Rails</title>
	<atom:link href="http://fuelyourcoding.com/category/languages/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://fuelyourcoding.com</link>
	<description></description>
	<lastBuildDate>Tue, 03 Jan 2012 11:01:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting Started with jQuery Mobile &amp; Rails 3</title>
		<link>http://fuelyourcoding.com/getting-started-with-jquery-mobile-rails-3/</link>
		<comments>http://fuelyourcoding.com/getting-started-with-jquery-mobile-rails-3/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 13:06:15 +0000</pubDate>
		<dc:creator>Jerod Santo</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery mobile]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://fuelyourcoding.com/?p=1341</guid>
		<description><![CDATA[<p><p><a href="http://rss.buysellads.com/click.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" target="_blank"><img src="http://rss.buysellads.com/img.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" border="0" alt="" /></a></p><p><a href="http://buysellads.com/buy/sitedetails/pubkey/f16d4ddc81a95a47348dcddb230bad58/zone/1271313" target="_blank">Advertise here via BSA</a></p></p>

&#60;head&#62;
  &#60;title&#62;Jqmoblog&#60;/title&#62;
  &#60;link rel=&#34;stylesheet&#34; href=&#34;http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.css&#34; /&#62;
  &#60;%= javascript_include_tag :defaults %&#62;
  &#60;script src=&#34;http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.js&#34;&#62;&#60;/script&#62;
  &#60;%= csrf_meta_tag %&#62;
&#60;/head&#62;

Okay, everything is in place. Now we&#8217;re ready to turn this into a mobile app!
Getting Started
Before we flesh out our mobile CRUD actions, let&#8217;s get the posts index looking decent in mobile browsers. jQuery Mobile relies [...]]]></description>
			<content:encoded><![CDATA[<p><p><a href="http://rss.buysellads.com/click.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" target="_blank"><img src="http://rss.buysellads.com/img.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" border="0" alt="" /></a></p><p><a href="http://buysellads.com/buy/sitedetails/pubkey/f16d4ddc81a95a47348dcddb230bad58/zone/1271313" target="_blank">Advertise here via BSA</a></p></p>
<p>The very promising <a href="http://jquerymobile.com/">jQuery Mobile</a> project is now in its first Alpha release! What better time to give it a spin with our (err&#8230; my) <a href="http://rubyonrails.org">favorite web framework</a>?!</p>
<p>You should read jQuery Mobile&#8217;s <a href="http://jquerymobile.com/demos/1.0a1/#docs/about/intro.html">official introduction</a> section to familiarize yourself with its principles, but in a few words: you include it on the page, format your markup to conform to its requirements and reap the benefits. Yay, let&#8217;s write a (very contrived) <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> application with Rails 3 and jQuery Mobile!</p>
<h2>Setup</h2>
<p>Our application will allow us to create, read, update and delete blog posts (remember when I said it was contrived? Uhm..yah) from mobile devices. If you don&#8217;t have Rails 3 installed yet, you can get it via <a href="http://rubygems.org">RubyGems</a>:</p>
<pre class="brush: plain;">
gem install rails
</pre>
<p>Use the <tt>rails</tt> command line tool to generate a new application. We&#8217;ll call it <strong>jqmoblog</strong>.</p>
<pre class="brush: plain;">
rails new jqmoblog
# ... a bunch of files are created
cd jqmoblog
</pre>
<p>As of Rails 3.0.1 you cannot specify jQuery as the JavaScript tool of choice when you generate a Rails application, but we can easily swap it in by using the handy, dandy <a href="http://github.com/indirect/jquery-rails">jquery-rails</a> gem. Edit the <tt>Gemfile</tt> in the project&#8217;s root directory and add a dependency on the gem. Once finished your <tt>Gemfile</tt> will look something like this:</p>
<pre class="brush: ruby;">
source 'http://rubygems.org'

gem 'rails', '3.0.1'
gem 'sqlite3-ruby', :require =&gt; 'sqlite3'
gem 'jquery-rails'
</pre>
<p>Install the required gems using the <tt>bundle</tt> command line tool:</p>
<pre class="brush: plain;">
bundle install
</pre>
<p>Now we&#8217;ll use the Rails generator added by the <tt>jquery-rails</tt> gem to get all the jQuery files we need:</p>
<pre class="brush: plain;">
rails generate jquery:install
</pre>
<p>When prompted, allow it to overwrite <tt>rails.js</tt>.</p>
<p>We&#8217;re almost ready. The application skeleton is in place and jQuery has replaced the Rails default JavaScript libraries, but we&#8217;re missing the basic model, controller, and views for our application.</p>
<p>Thankfully, Rails has a generator which will create these files for us and its called the <tt>scaffold</tt> generator. We&#8217;ll keep the blog posts in our app super-simple since we&#8217;re really just using them to learn about jQuery Mobile. Each post will have a title and a body. Generate the scaffold for a Post resource like this:</p>
<pre class="brush: plain;">
rails generate scaffold Post title:string body:text
</pre>
<p>This command will provide a list of files and directories it created. There should be a model (<tt>app/models/post.rb</tt>), a controller (<tt>app/controller/posts_controller.rb</tt>), and a directory of views (<tt>app/views/posts</tt>) for our Post resource.</p>
<p>Run the database migration that was created from the <tt>scaffold</tt> command so our database has a <tt>posts</tt> table with the required columns for storing our posts:</p>
<pre class="brush: plain;">
rake db:migrate
</pre>
<p>At this point you can fire up a Rails server and the app will load up in your browser:</p>
<pre class="brush: plain;">
rails server
</pre>
<p>Point your browser to <tt>http://localhost:3000</tt> and you should see this:</p>
<p style="text-align: center;"><img src="http://fuelyourcoding.com/files/default.png" alt="default" title="default" width="549" height="518" class="aligncenter size-full wp-image-1363" /></p>
<p>Hmm, before we get to mobilizing this application, let&#8217;s make the root url load the list of our posts instead of the default Rails index file:</p>
<pre class="brush: plain;">
rm public/index.html
</pre>
<p>Edit the <tt>config/routes.rb</tt> and set the root route like so:</p>
<pre class="brush: ruby;">
Jqmoblog::Application.routes.draw do
  resources :posts
  root :to =&gt; &quot;posts#index&quot;
end
</pre>
<p>Now load up <tt>http://localhost:3000</tt> again and you should see the empty list of posts.</p>
<p>We still need to get the jQuery Mobile files on the page. Lets use the ones hosted on <a href="http://jquery.com">jquery.com</a>. Add them to the application&#8217;s <tt>layout</tt> file so they&#8217;ll be on every page that gets served. In <tt>app/views/layouts/application.html.erb</tt>, edit the <tt>head</tt> section to look like this:</p>
<pre class="brush: xml;">
&lt;head&gt;
  &lt;title&gt;Jqmoblog&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.css&quot; /&gt;
  &lt;%= javascript_include_tag :defaults %&gt;
  &lt;script src=&quot;http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0a1.min.js&quot;&gt;&lt;/script&gt;
  &lt;%= csrf_meta_tag %&gt;
&lt;/head&gt;
</pre>
<p>Okay, everything is in place. <em>Now</em> we&#8217;re ready to turn this into a mobile app!</p>
<h2>Getting Started</h2>
<p>Before we flesh out our mobile CRUD actions, let&#8217;s get the posts index looking decent in mobile browsers. jQuery Mobile relies directly on the page&#8217;s markup to work its magic, so to invoke it we just need to change our views to conform to its conventions.</p>
<p>Each &#8220;page&#8221; in a jQuery Mobile application needs to be wrapped in a <tt>div</tt> with a <tt>data-role</tt> attribute of <tt>page</tt>. I put &#8220;page&#8221; in quotes because there can actually be many of them on a single webpage loaded from the server. In our app we&#8217;ll be loading each &#8220;page&#8221; from Rails, so we should just add this to our layout file which will wrap each of our individual views:</p>
<p><strong>Before</strong></p>
<pre class="brush: xml;">
&lt;body&gt;
  &lt;%= yield %&gt;
&lt;/body&gt;
</pre>
<p><strong>After</strong></p>
<pre class="brush: xml;">
&lt;body&gt;
  &lt;div data-role=&quot;page&quot;&gt;
    &lt;%= yield %&gt;
  &lt;/div&gt;
&lt;/body&gt;
</pre>
<p>Next edit the post&#8217;s index (<tt>index.html.erb</tt>) and change the markup to create a header and a content section which lists all the posts in the database.</p>
<p><strong>Before</strong></p>
<pre class="brush: xml;">
&lt;h1&gt;Listing posts&lt;/h1&gt;

&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Title&lt;/th&gt;
    &lt;th&gt;Body&lt;/th&gt;
    &lt;th&gt;&lt;/th&gt;
    &lt;th&gt;&lt;/th&gt;
    &lt;th&gt;&lt;/th&gt;
  &lt;/tr&gt;

&lt;% @posts.each do |post| %&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;%= post.title %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= post.body %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= link_to 'Show', post %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= link_to 'Edit', edit_post_path(post) %&gt;&lt;/td&gt;
    &lt;td&gt;&lt;%= link_to 'Destroy', post, :confirm =&gt; 'Are you sure?', :method =&gt; :delete %&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;% end %&gt;
&lt;/table&gt;

&lt;br /&gt;

&lt;%= link_to 'New Post', new_post_path %&gt;
</pre>
<p><strong>After</strong></p>
<pre class="brush: xml;">
&lt;div data-role=&quot;header&quot;&gt;
  &lt;h1&gt;Listing posts&lt;/h1&gt;
&lt;/div&gt;

&lt;div data-role=&quot;content&quot;&gt;
  &lt;ul data-role=&quot;listview&quot;&gt;
    &lt;% @posts.each do |post| %&gt;
    &lt;li&gt;
      &lt;%= link_to post.title, post %&gt;
      &lt;%= link_to 'edit post', edit_post_path(post), &quot;data-icon&quot; =&gt; &quot;gear&quot; %&gt;
    &lt;/li&gt;
    &lt;% end %&gt;
  &lt;/ul&gt;
&lt;/div&gt;
</pre>
<p>Load up the page and it should look like this:</p>
<p style="text-align: center;"><img src="http://fuelyourcoding.com/files/base.png" alt="base" title="base" width="472" height="615" class="aligncenter size-full wp-image-1365" /></p>
<p>Two things to note here. First, we are once again using the <tt>data-role</tt> attribute to create the header and the styled list. This is a major theme of jQuery Mobile (as of the Alpha, I&#8217;m told they <em>*might*</em> be switching to class names instead of data- attributes). Second, each list item has two links, and jQuery Mobile automatically knows that this means we want to use a <a href="http://jquerymobile.com/demos/1.0a1/#docs/lists/lists-split.html">split button list</a>. Pretty cool, huh?</p>
<p>Let&#8217;s add a button which will take us to a page where we can create new posts. Add this line to the end of the header <tt>div</tt>:</p>
<pre class="brush: xml;">
&lt;%= link_to 'Add', new_post_path, &quot;data-icon&quot; =&gt; &quot;plus&quot;, &quot;class&quot; =&gt; &quot;ui-btn-right&quot; %&gt;
</pre>
<p>Again, jQuery Mobile &#8220;just knows&#8221; that this should be a button in our header and we pass it a class and a <tt>data-icon</tt> attribute to tell it where to place it and which icon to use. We now have a button in the header which links to the new post path!</p>
<p style="text-align: center;"><img src="http://fuelyourcoding.com/files/base-add.png" alt="base-add" title="base-add" width="472" height="615" class="aligncenter size-full wp-image-1366" /></p>
<h2>Create</h2>
<p>We&#8217;re gonna need to create posts so we can read, update and delete them, so lets handle creation first. The &#8220;Add&#8221; button on the posts index links to the <tt>new.html.erb</tt> view. It works as is and looks okay too. We&#8217;ll just tweak the markup to make it fit into the jQuery Mobile styles:</p>
<pre class="brush: xml;">
&lt;div data-role=&quot;header&quot;&gt;
  &lt;h1&gt;New Post&lt;/h1&gt;
&lt;/div&gt;

&lt;div data-role=&quot;content&quot;&gt;
  &lt;%= render 'form' %&gt;
&lt;/div&gt;
</pre>
<p>We can now add new posts using the mobile interface! Go ahead and create a few so the posts index shows a list with links to edit each post:</p>
<p style="text-align: center;"><img src="http://fuelyourcoding.com/files/base-posts.png" alt="base-posts" title="base-posts" width="472" height="615" class="aligncenter size-full wp-image-1367" /></p>
<h2>Read</h2>
<p>People will read posts from their <tt>show.html.erb</tt> view. Again, no change to the underlying controller or model code needs to happen. Just update the view file to look like this:</p>
<pre class="brush: xml;">
&lt;div data-role=&quot;header&quot;&gt;
  &lt;h1&gt;&lt;%= @post.title %&gt;&lt;/h1&gt;
  &lt;%= link_to 'Home', posts_url, &quot;class&quot; =&gt; &quot;ui-btn-right&quot; %&gt;
&lt;/div&gt;

&lt;div data-role=&quot;content&quot;&gt;
  &lt;%= @post.body %&gt;
&lt;/div&gt;
</pre>
<p style="text-align: center;"><img src="http://fuelyourcoding.com/files/post-show.png" alt="post-show" title="post-show" width="472" height="615" class="aligncenter size-full wp-image-1368" /></p>
<h2>Update</h2>
<p>When we set up the posts index we added links to the <tt>edit.html.erb</tt> view using the gear icon on the right side of the split list. Now we just need to change the markup on that view to conform to jQuery Mobile&#8217;s requirements. Have you sniffed out a pattern to this development process yet?</p>
<pre class="brush: xml;">
&lt;div data-role=&quot;header&quot;&gt;
  &lt;h1&gt;Editing Post&lt;/h1&gt;
&lt;/div&gt;

&lt;div data-role=&quot;content&quot;&gt;
  &lt;%= render 'form' %&gt;
&lt;/div&gt;
</pre>
<p>Again, it just works as expected and it will look great on many mobile devices. On to the final action, delete!</p>
<h2>Delete</h2>
<p>To facilitate deleting a post we just have to modify the delete links created by Rails&#8217; scaffold generator to fit the rest of the mobile site. I think the edit page is a good place to add a delete link, so lets add a button to the header of <tt>edit.html.erb</tt>:</p>
<pre class="brush: xml;">
&lt;%= link_to 'Delete', @post, :method =&gt; :delete, &quot;data-icon&quot; =&gt; &quot;delete&quot;, &quot;class&quot; =&gt; &quot;ui-btn-right&quot; %&gt;
</pre>
<p>The edit view is now complete, and it lets us update and delete our post.</p>
<p style="text-align: center;"><img src="http://fuelyourcoding.com/files/post-edit.png" alt="post-edit" title="post-edit" width="472" height="615" class="aligncenter size-full wp-image-1369" /></p>
<h2>Closing Up</h2>
<p>If you&#8217;ve made it this far, you&#8217;ve probably realized that working with jQuery Mobile is pretty painless and it fits in with Rails quite well. There are a bunch of other things that jQuery Mobile can do, but this should hopefully whet your appetite. Remember, the framework is still in the <strong>Alpha</strong> stage so things can and probably will change.</p>
<p>I encourage you to <a href="http://jquerymobile.com/">read more</a> about the project and get involved at an early stage so we can all benefit from a strong community!</p>
<p style="text-align: center;"><a href="http://fuelyourcoding.com/files/jqmoblog.zip">***Download the source code for the application built in this post***</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fuelyourcoding.com/getting-started-with-jquery-mobile-rails-3/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Set Rails Logging on Fire</title>
		<link>http://fuelyourcoding.com/set-rails-logging-on-fire/</link>
		<comments>http://fuelyourcoding.com/set-rails-logging-on-fire/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 17:10:14 +0000</pubDate>
		<dc:creator>Jerod Santo</dc:creator>
				<category><![CDATA[Plugins / Add-Ons]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[firebug]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://fuelyourcoding.com/?p=842</guid>
		<description><![CDATA[<p><p><a href="http://rss.buysellads.com/click.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" target="_blank"><img src="http://rss.buysellads.com/img.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" border="0" alt="" /></a></p><p><a href="http://buysellads.com/buy/sitedetails/pubkey/f16d4ddc81a95a47348dcddb230bad58/zone/1271313" target="_blank">Advertise here via BSA</a></p></p>
]]></description>
			<content:encoded><![CDATA[<p><p><a href="http://rss.buysellads.com/click.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" target="_blank"><img src="http://rss.buysellads.com/img.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" border="0" alt="" /></a></p><p><a href="http://buysellads.com/buy/sitedetails/pubkey/f16d4ddc81a95a47348dcddb230bad58/zone/1271313" target="_blank">Advertise here via BSA</a></p></p>
<p>Rails 2.3+ and Rails 3 rely on <a href="http://rack.rubyforge.org/">Rack</a>, a minimal (and awesome) interface between Ruby and webservers. This has many advantages, one of which is the ability to easily swap Rack applications (middlewares) in &#038; out of your Rails app. Modularity FTW!</p>
<p>One fun (and useful) example of a Rack middleware is a Firebug logger written by <a href="http://sjjdev.com">Simon Jefford</a> for the <a href="http://coderack.org/">CodeRack</a> competition. This middleware allows you to send arbitrary messages directly to <a href="http://getfirebug.com/">Firebug</a>. Why? Since you&#8217;re already debugging much of your Rails app in a browser anyway, sending debug output to your browser&#8217;s console (instead of tailing a log file) makes a lot of sense.</p>
<p><em>NOTE: FirebugLogger plays nice with WebKit&#8217;s Web Inspector as well.</em></p>
<p>Simon also released a <a href="http://github.com/simonjefford/rack_firebug_logger">Rails plugin</a> that you can install to set up the middleware for you, but it&#8217;s more fun (and informative) to set it up yourself. Here&#8217;s how:</p>
<h2>Setup</h2>
<p>The goal is to be able to send arbitrary messages to Firebug from any controller or view.</p>
<p>Rails autoloads (and namespaces) any code placed in the <tt>lib</tt> directory, so that is where we&#8217;ll place our FirebugLogger code. Grab the code from <a href="http://gist.github.com/252575">my gist</a>, which is a fork of <a href="http://gist.github.com/210069">Simon&#8217;s original</a> with minor improvements, and place it inside your Rails app in:</p>
<pre class="brush: plain;">
lib/rack/firebug_logger.rb
</pre>
<p>Rails will load the code for us, but we need to manually activate the middleware. Since this bit of code is only useful during development, we&#8217;ll load it up in that environment only.</p>
<pre class="brush: ruby;">
# config/environments/development.rb
# ... other stuff ...
config.middleware.use ::Rack::FirebugLogger
</pre>
<h2>Using FirebugLogger</h2>
<p>Using the middleware is pretty straight forward. It will process an array of arrays, each of which has a log level and a message. Add a single line like the one below to an existing controller action and load up the page:</p>
<pre class="brush: ruby;">
# app/controllers/posts_controller.rb
class PostsController &lt; ApplicationController
  def index
    @posts = Post.all
    request.env['firebug.logs'] = [[:info, &quot;hello from rack!&quot;]]
  end
  # ... other actions ...
end
</pre>
<p>Open Firebug and you should see &#8220;hello from rack!&#8221; glaring back at you. You can do the same thing in views:</p>
<pre class="brush: ruby;">
# app/views/posts/index.html.erb
&lt;% request.env['firebug.logs'] = [[:warn, &quot;inside a view!&quot;]] %&gt;
</pre>
<h2>Adding a Helper</h2>
<p>Using the logger like this is a bit tedious, and it&#8217;s not easy to create multiple messages for a single request. Let&#8217;s wrap the functionality up into a method that we can call. This method should be placed in the application controller so that all other controllers inherit it.</p>
<pre class="brush: ruby;">
# app/controllers/application_controller.rb
class ApplicationController &lt; ActionController::Base
# ... other stuff ...
helper_method :firebug

  private

  def firebug(message,type = :debug)
    request.env['firebug.logs'] ||= []
    request.env['firebug.logs'] &lt;&lt; [type.to_sym, message.to_s]
  end
end
</pre>
<p>This method will initialize an empty array the first time it is called and then push new messages on to the array on subsequent calls. Notice that it calls <tt>to_s</tt> on the <tt>message</tt> variable before passing it on. This means you can send the method a string or any object that responds to <tt>to_s</tt> and it will just work. Specifying a log type is optional, the method is private and it is explicitly added as a <tt>helper_method</tt> so that you can access it from views as well.</p>
<p>Now writing logs to Firebug is as easy as:</p>
<pre class="brush: ruby;">
firebug &quot;woop woop!&quot;
# optionally specify a log level
firebug &quot;it's a trap!&quot;, :warning
</pre>
<p>Here are a few ideas of helpful messages to send to Firebug:</p>
<pre class="brush: ruby;">
# inspect the attributes of an object
firebug @posts.first.inspect
# dump the session
firebug session
# check a user's roles
firebug current_user.roles.inspect
</pre>
<p>I highly encourage you to try this in one of your Rails apps. It has proven a useful addition to my toolkit. Let me know how it works for you by leaving a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://fuelyourcoding.com/set-rails-logging-on-fire/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Creating Your First Ruby on Rails Application From Scratch</title>
		<link>http://fuelyourcoding.com/creating-your-first-ruby-on-rails-application-from-scratch/</link>
		<comments>http://fuelyourcoding.com/creating-your-first-ruby-on-rails-application-from-scratch/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 16:14:31 +0000</pubDate>
		<dc:creator>Mageshcse</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://fuelyourcoding.com/?p=495</guid>
		<description><![CDATA[<p><p><a href="http://rss.buysellads.com/click.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" target="_blank"><img src="http://rss.buysellads.com/img.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" border="0" alt="" /></a></p><p><a href="http://buysellads.com/buy/sitedetails/pubkey/f16d4ddc81a95a47348dcddb230bad58/zone/1271313" target="_blank">Advertise here via BSA</a></p></p>
]]></description>
			<content:encoded><![CDATA[<p><p><a href="http://rss.buysellads.com/click.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" target="_blank"><img src="http://rss.buysellads.com/img.php?z=1271313&k=f16d4ddc81a95a47348dcddb230bad58&a=<?php echo($a); ?>&c=<?php echo(rand()); ?>" border="0" alt="" /></a></p><p><a href="http://buysellads.com/buy/sitedetails/pubkey/f16d4ddc81a95a47348dcddb230bad58/zone/1271313" target="_blank">Advertise here via BSA</a></p></p>
<p>This tutorial which will help you create a basic Ruby on Rails application, without worrying too much about Ruby basics for now. Well, if you want to learn more about Ruby or Rails basic concepts, please refer the following links:</p>
<ul>
<li><a href="http://guides.rubyonrails.org/">http://guides.rubyonrails.org/</a></li>
<li><a href="http://www.rubyonrailstutorials.com/">http://www.rubyonrailstutorials.com/</a></li>
<li><a href="http://railstutor.com/">http://railstutor.com/</a></li>
</ul>
<p>And if you need more help, you can hit the #railsbridge channel of <a href="http://en.wikipedia.org/wiki/Freenode">freenode</a><a href="http://en.wikipedia.org/wiki/Internet_Relay_Chat">IRC</a>.</p>
<p>So what are you waiting for? Lets get started with creating our basic rails application!</p>
<h2>Getting Started</h2>
<p><strong>What is Ruby</strong></p>
<p><strong> </strong>Ruby is one of the easiest scripting languages I have ever seen; simple enough for non-tech people, powerful enough for advanced programmers and best of all, it doesn&#8217;t have a complex syntax or keywords to be memorized. Instead it is written in a simple English syntax.</p>
<p><strong>What is Rails</strong></p>
<p><strong> </strong>Rails is a web development framework written in the Ruby language, which has a general MVC (Models, Views, Controllers) type of structure to make everything simple for the developers. It allows you to write less code but accomplish more than most other languages/frameworks.</p>
<h2>Rails MVC Architecture</h2>
<p><img class="alignnone size-full wp-image-499" src="http://fuelyourcoding.com/files/image2.png" alt="image2" width="400" height="291" /></p>
<p>Rails has a MVC (Models, Views, Controllers) architecture and its benefits include:</p>
<li>Isolation of business logic from the user interface</li>
<li>Ease of keeping code DRY</li>
<li>Making it clear where different types of code belong for easier maintenance</li>
<p><em>From </em><a href="http://guides.rubyonrails.org/getting_started.html" target="_blank"><em>Getting Started With Rails</em></a></p>
<blockquote><p><strong>Models</strong></p>
<p><strong> </strong>A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. Most often, a single table in your database will correspond to a single model in your application. The bulk of your application&#8217;s business logic will be concentrated in the models.</p>
<p><strong>Views</strong></p>
<p><strong> </strong>Views represent the user interface of your application. In Rails, views are often HTML files with embedded Ruby code that performs tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.</p>
<p><strong>Controllers</strong></p>
<p><strong> </strong>Controllers provide the &#8220;glue&#8221; between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.<span style="color: #ff0000"><br />
</span></p></blockquote>
<h2>Welcome to Rails!</h2>
<p>Now lets create the sample &#8216;Slambook&#8217; Application.<span style="color: #ff0000"> </span></p>
<p><strong>Requirements:</strong></p>
<p><strong> </strong></p>
<li>Install <a href="http://ruby-lang.org">Ruby</a> and RubyGems</li>
<li>Install <a href="http://www.rubyonrails.org">Rails</a></li>
<li>Install SQLite, Postgres or MySQL</li>
<p>(Note: I will be using ubuntu (linux) and Postgres for building my rails application, but you can also install rails on your Windows or Mac machine as well.]</p>
<p>Once you have everything installed, we can start creating our rails application.  To begin, open a terminal (Console, Command Prompt, etc) and type the command:</p>
<pre class="brush: bash;"> rails slambook </pre>
<p>For this example we will use &#8217;slambook&#8217; as the name of the application which we will build. As soon as you hit enter, you will see the scripts flow in your terminal creating a bunch of files (as seen in the picture below):</p>
<p><img class="size-medium wp-image-500 alignnone" src="http://fuelyourcoding.com/files/image3-600x375.png" alt="image3" width="600" height="375" /></p>
<p>These are the files which rails automatically generates to create the framework for our application.</p>
<p>Now just change the directory to our application using:</p>
<pre class="brush: bash;"> cd guestbook </pre>
<p>and feel free to explore all the sub-directories and the files inside it to get an idea where the code lives.</p>
<p><img class="size-medium wp-image-501 alignnone" src="http://fuelyourcoding.com/files/image4-600x498.png" alt="image4" width="600" height="498" /></p>
<p>Now,  lets test if your rails application is working by using the command:</p>
<pre class="brush: bash;">script/server </pre>
<p>Open your favourite browser and type <a href="http://localhost:3000/">http://localhost:3000/</a> in the address bar and press Enter.</p>
<p><img class="alignnone size-medium wp-image-502" src="http://fuelyourcoding.com/files/image5-600x237.png" alt="image5" width="600" height="237" /></p>
<p><img class="alignnone size-medium wp-image-503" src="http://fuelyourcoding.com/files/image6-600x471.png" alt="image6" width="600" height="471" /></p>
<p>If you see a page similar to the above image, then your first rails application works fine (pat yourself on the back, you got it right)!</p>
<p>To start off, we are going to create the needed controller, model and views for our guestbooks post functionality which allows your friends to sign your &#8217;slambook&#8217;.</p>
<p>Using the command:</p>
<pre class="brush: bash;"> $script/generate scaffold post name:string address:text dob:date desire:text interests:text hobby:text signature:text </pre>
<p><img class="alignnone size-medium wp-image-504" src="http://fuelyourcoding.com/files/image7-600x300.png" alt="image7" width="600" height="300" /></p>
<p>Scaffold command creates a CRUD (Create, Read, Update, Delete) interface for your app ( a quick way of creating the MVC automatically). Alternatively, you can also create your controller, model and view manually using the command</p>
<pre class="brush: bash;"> $script/generate controller &lt;controller name&gt; // for creating controller and views </pre>
<pre class="brush: bash;"> $script/generate model &lt;model name&gt; // for creating model </pre>
<p>Here we are telling rails that we want code generated to handle a &#8220;Post&#8221; model, which has a string type for name and text for address, date for dob and so on. Notice this will generate some more files, one of which is the database migration file found inside the db/ directory</p>
<p><img class="alignnone size-medium wp-image-505" src="http://fuelyourcoding.com/files/image8-600x406.png" alt="image8" width="600" height="406" /></p>
<p>This is how our first migration file looks (slambook application):</p>
<p>The migration files are used for managing the CRUD (create, read, update and delete) activities of the database. If you haven&#8217;t created your database yet we can do it by a command:</p>
<pre class="brush: bash;"> $rake db:create </pre>
<p>Now you might wonder how rails connects our database, and for this purpose we provide our database information in the database.yml file which is found inside the config/ directory:</p>
<p><img class="alignnone size-full wp-image-506" src="http://fuelyourcoding.com/files/image9.png" alt="image9" width="507" height="199" /></p>
<p>This is how the database.yml file looks. We mention our database name and user details for rails to make the connection.</p>
<p>Since now we have recently created a new model for Post, a table must be created in our database and requires that we upgrade the database using this command:</p>
<pre class="brush: bash;"> $rake db:migrate // (instead of creating/updating the database table manually) </pre>
<p>This updates the database with the information from the migration file.</p>
<p>(Note: when this command is executed, the migration file inside the db/migrate/ directory say 20090718013114_create_posts.rb is used as an reference for updating the database)</p>
<p>Now lets checkout what we have done so far:</p>
<pre class="brush: bash;"> $script/server </pre>
<p>As before, open your browser and type <a href="http://localhost:3000/posts">http://localhost:3000/posts</a> in the address bar and press Enter to see your application:</p>
<p><img class="alignnone size-medium wp-image-507" src="http://fuelyourcoding.com/files/image10-600x270.png" alt="image10" width="600" height="270" /></p>
<p>If your page looks like that, then shout &#8220;Wow! I have created my first rails application!&#8221;</p>
<p>The page at <a href="http://localhost:3000/posts">http://localhost:3000/posts</a> shows the list of post made in your guestbook. Note there you have a link &#8220;New Post&#8221; which links to the the page at <a href="http://localhost/posts/new">http://localhost/posts/new</a> where your friends can sign your guestbook. You must wonder how these pages were created, so take a look at the app/views/posts directory which contains the html pages that were created by the scaffold command. These html pages have .html.erb extension which means ruby code can be embedded in your html files.</p>
<p>Now lets create a home page for our &#8217;slambook&#8217; application:</p>
<pre class="brush: bash;"> $script/generate controller home index </pre>
<p>This creates a controller &#8220;home&#8221; along with views in the app/view/home directory. Rails will create several files for you, including app/views/home/index.html.erb file. This is the template that we will use to display the results of the index action (method) in the home controller. Open this file in your text editor and edit it to contain the code that you want to display in your index page:</p>
<pre class="brush: ruby;"> &lt;h1&gt;Magesh's Slam-book or Guestbook&lt;/h1&gt; &lt;%= link_to &quot;View my book&quot;, posts_path %&gt; &lt;%= link_to &quot;Sign my book&quot;, new_post_path %&gt; </pre>
<p>The last two lines have added two links in my home page, one for people to view all the posts in my slambook and the other for new friends to use to sign my slambook.</p>
<p> tag is used to display the link. posts_path links to the posts/index page and new_post_path links to the posts/new page.</p>
<p>I suggest you also open the other .html.erb files in app/views/posts directory like index.html.erb, show.html.erb, edit.html.erb and new.html.erb to have a look around. Their functions are described in the file posts_controller.rb (controller) inside the app/controller directory.</p>
<p><img class="alignnone size-medium wp-image-508" src="http://fuelyourcoding.com/files/image11-600x427.png" alt="image11" width="600" height="427" /></p>
<p>Dont get confused with the above image, I will tell you how it works:</p>
<p>Lets take the index method first:</p>
<pre class="brush: ruby;">
def index @posts = Post.all respond_to do |format|
format.html # index.html.erb
format.xml { render :xml =&gt; @posts }
end
</pre>
<p>@posts variable stores all the posts and format.html is used to render the index.html.erb file in the app/views/posts directory.</p>
<p>Similarly the other methods such as new, edit and show are created. Notice that the methods inside the posts_controller.rb file are related to the files inside app/views/posts directory.</p>
<p>The show method renders the app/views/posts/show.html.erb file and in the same way the edit method renders the app/views/post/edit.html.erb file.</p>
<p>When you hit <a href="http://localhost:3000/">http://localhost:3000/</a> in your browser you should get the rails default page instead of our home page. That is because rails has created a default index.html page in the public directory. Since we want to use the home controller to show the index page instead of the rails default page ( public/index.html ), we must delete that public/index.html file first:</p>
<pre class="brush: bash;"> $rm public/index.html </pre>
<p>and then tell rails to point to home controller as the default index page. That is done by editing the config/routes.rb file (rails routing)</p>
<h2>Rails routing?</h2>
<p>You can learn more about routing by reading <a href="http://guides.rubyonrails.org/routing.html">Rails Routing from the Outside In</a>.</p>
<p>Edit config/routes.rb in your favorite editor (I prefer emacs)</p>
<pre class="brush: ruby;"> ActionController::Routing::Routes.draw do |map|
map.resources :posts
map.resources :home
map.root :controller =&gt; &quot;home&quot;;
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
</pre>
<p>Note: Whenever a controller is created we need to inform rails about it using the map.resources :  which creates the url say <a href="http://localhost/">http://localhost/</a> in our application.</p>
<p>Also check out the 4th line: map.root :controller =&gt; &#8220;home&#8221; tells rails to load the home controller for the default home page.</p>
<p>Now visit <a href="http://localhost:3000/">http://localhost:3000/</a> and you should see our new home page:</p>
<p><img class="alignnone size-medium wp-image-509" src="http://fuelyourcoding.com/files/image12-600x231.png" alt="image12" width="600" height="231" /></p>
<h2>Conclusion</h2>
<p>So this is just the skeleton of our application. We obviously should add CSS to design our rails application to make everything look better!</p>
<p>I hope this helps you start Ruby on Rails with ease! If you have any queries or are facing any problem while trying out this on your machine please feel free to shout your comments below. ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://fuelyourcoding.com/creating-your-first-ruby-on-rails-application-from-scratch/feed/</wfw:commentRss>
		<slash:comments>53</slash:comments>
		</item>
	</channel>
</rss>

