<?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>Mon, 09 Aug 2010 14:40:31 +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>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[Rails 2.3+ and Rails 3 rely on Rack, 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!
One fun (and useful) example of a Rack middleware is a Firebug logger written [...]<p><p><strong>Sponsored by</strong></p>
<a href='http://madebytinder.com' target='_blank'><img src='http://fuelbrand.s3.amazonaws.com/downloads/WhatisTinder250x250.jpg' border='0' alt='Made By Tinder' /></a>
<p><a href="http://www.fuelbrandnetwork.com/advertise/">Advertise on Fuel Brand Network</a>. <br />
  <a href="http://www.fuelbrandnetwork.com">Fuel Brand Network</a> 2010 <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/">cc</a> (creative commons license)
</p></p>
<p><a href="http://fuelyourcoding.com/set-rails-logging-on-fire/">Set Rails Logging on Fire</a></p>
]]></description>
			<content:encoded><![CDATA[<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>
<p><p><strong>Sponsored by</strong></p>
<a href='http://madebytinder.com' target='_blank'><img src='http://fuelbrand.s3.amazonaws.com/downloads/WhatisTinder250x250.jpg' border='0' alt='Made By Tinder' /></a>
<p><a href="http://www.fuelbrandnetwork.com/advertise/">Advertise on Fuel Brand Network</a>. <br />
  <a href="http://www.fuelbrandnetwork.com">Fuel Brand Network</a> 2010 <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/">cc</a> (creative commons license)
</p></p>
<p><a href="http://fuelyourcoding.com/set-rails-logging-on-fire/">Set Rails Logging on Fire</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fuelyourcoding.com/set-rails-logging-on-fire/feed/</wfw:commentRss>
		<slash:comments>8</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[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:

http://guides.rubyonrails.org/
http://www.rubyonrailstutorials.com/
http://railstutor.com/

And if you need more help, you can hit the #railsbridge channel of freenodeIRC.
So what are [...]<p><p><strong>Sponsored by</strong></p>
<a href='http://madebytinder.com' target='_blank'><img src='http://fuelbrand.s3.amazonaws.com/downloads/WhatisTinder250x250.jpg' border='0' alt='Made By Tinder' /></a>
<p><a href="http://www.fuelbrandnetwork.com/advertise/">Advertise on Fuel Brand Network</a>. <br />
  <a href="http://www.fuelbrandnetwork.com">Fuel Brand Network</a> 2010 <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/">cc</a> (creative commons license)
</p></p>
<p><a href="http://fuelyourcoding.com/creating-your-first-ruby-on-rails-application-from-scratch/">Creating Your First Ruby on Rails Application From Scratch</a></p>
]]></description>
			<content:encoded><![CDATA[<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>
<p><p><strong>Sponsored by</strong></p>
<a href='http://madebytinder.com' target='_blank'><img src='http://fuelbrand.s3.amazonaws.com/downloads/WhatisTinder250x250.jpg' border='0' alt='Made By Tinder' /></a>
<p><a href="http://www.fuelbrandnetwork.com/advertise/">Advertise on Fuel Brand Network</a>. <br />
  <a href="http://www.fuelbrandnetwork.com">Fuel Brand Network</a> 2010 <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/us/">cc</a> (creative commons license)
</p></p>
<p><a href="http://fuelyourcoding.com/creating-your-first-ruby-on-rails-application-from-scratch/">Creating Your First Ruby on Rails Application From Scratch</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fuelyourcoding.com/creating-your-first-ruby-on-rails-application-from-scratch/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
	</channel>
</rss>
