<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rails on Blues</title>
	<atom:link href="http://railsonblues.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://railsonblues.wordpress.com</link>
	<description>Learning Rails while listening Blues :)</description>
	<lastBuildDate>Tue, 06 Oct 2009 07:40:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='railsonblues.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Rails on Blues</title>
		<link>http://railsonblues.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://railsonblues.wordpress.com/osd.xml" title="Rails on Blues" />
	<atom:link rel='hub' href='http://railsonblues.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Hello World from Controller</title>
		<link>http://railsonblues.wordpress.com/2009/10/06/hello-world-from-controller/</link>
		<comments>http://railsonblues.wordpress.com/2009/10/06/hello-world-from-controller/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 07:40:50 +0000</pubDate>
		<dc:creator>railsonblues</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://railsonblues.wordpress.com/?p=14</guid>
		<description><![CDATA[In previous post we have seen how to say &#8216;Hello World&#8217; from View in rails application. Now to step little bit further, we will introduce controller in action. Though it was already part of process in previous post, but &#8216;index&#8217; action was empty, and view held actual text to display. So idea now is to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=railsonblues.wordpress.com&amp;blog=9570340&amp;post=14&amp;subd=railsonblues&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In previous post we have seen how to say &#8216;Hello World&#8217; from View in rails application. Now to step little bit further, we will introduce controller in action. Though it was already part of process in previous post, but &#8216;index&#8217; action was empty, and view held actual text to display. So idea now is to use same application and extend it little.</p>
<p>If you don&#8217;t already have written previous hello world application you can do it now by completing steps 1-6.</p>
<p>1) First step in addition to previous six is creating new action inside &#8216;say&#8217; controller, we will give it different name, for example &#8216;hello&#8217;. So open file &#8216;\helloworld\app\controller\say_controller.rb&#8217; in notepad and add next method:<br />
     <code><br />
     def hello<br />
          @message = "Hello World from Controller"<br />
     end<br />
     </code><br />
This will need some explanation, &#8216;@message&#8217; is field that we are declaring in controller, its convention to give fields prefix &#8216;@&#8217;. Good part here is that this field will be visible from view that we will create in next step.</p>
<p>2) So keeping previous talk about controllers we have a clue that &#8216;hello&#8217; token can now be part of url in web browser, and we also now that we will need new view with same name. That view we have to create inside &#8216;helloworld\app\views\say\&#8217; and name it &#8216;hello.html.erb&#8217;, its content will be:<br />
     <code><br />
     &lt;h1&gt;&lt;%= @message %&gt;&lt;/h1&gt;<br />
     </code><br />
So here we have mixture of html code and ruby code (called erb). We see that embedding ruby code is done by specifying it between &#39;&lt;%&#39; and &#39;%&gt;&#8217;. Yeah i haven&#8217;t forgot to include &#8216;=&#8217; sign, reason for that is because there is difference in wrapping ruby code between &#8216;&lt;%=&#39; and &#39;&lt;%&#39;. Difference is that first one will produce output from view template, and second one will not. So when we want to output something in html, we will use &#39;&lt;%=&#39;, and when we want to wrap ruby statements without any output we use &#39;&lt;%&#39; (common use of second one is for wrapping loops).</p>
<p>3) Now we are ready to try our new controller action and view, so open web browser and enter next url:<br />
	http://localhost:3000/say/hello<br />
Note: WEBrick should be working, if you turned it of use step 7 from previous post for starting.</p>
<p>4) Now we have different output in web browser, its because we are now requesting different action inside &#39;say&#39; controller.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/railsonblues.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/railsonblues.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/railsonblues.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/railsonblues.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/railsonblues.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/railsonblues.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/railsonblues.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/railsonblues.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/railsonblues.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/railsonblues.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/railsonblues.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/railsonblues.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/railsonblues.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/railsonblues.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=railsonblues.wordpress.com&amp;blog=9570340&amp;post=14&amp;subd=railsonblues&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://railsonblues.wordpress.com/2009/10/06/hello-world-from-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ee36179a6041c88c313d9021b7a348f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">railsonblues</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello World from View</title>
		<link>http://railsonblues.wordpress.com/2009/10/05/hello-world-from-view/</link>
		<comments>http://railsonblues.wordpress.com/2009/10/05/hello-world-from-view/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 19:54:39 +0000</pubDate>
		<dc:creator>railsonblues</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://railsonblues.wordpress.com/?p=3</guid>
		<description><![CDATA[1)  First step is choosing folder on file system for keeping rails applications together. For example I will use folder named ‘rails_apps’ in root of c drive. To accomplish this open command prompt window and type next four lines: c: cd \ md rails_apps cd rails_apps 2) Now when we are inside ‘rails_apps’ folder, we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=railsonblues.wordpress.com&amp;blog=9570340&amp;post=3&amp;subd=railsonblues&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>1)  First step is choosing folder on file system for keeping rails applications together. For example I will use folder named ‘rails_apps’ in root of c drive. To accomplish this open command prompt window and type next four lines:<br />
<code><br />
c:<br />
cd \<br />
md rails_apps<br />
cd rails_apps<br />
</code><br />
2) Now when we are inside ‘rails_apps’ folder, we will create structure of rails application by executing:<br />
<code><br />
rails helloworld<br />
</code><br />
3) Previous step created for us new folder named ‘helloworld’ inside ‘rails_apps’, we will have to navigate inside it before executing next steps:<br />
<code><br />
cd helloworld<br />
</code><br />
4) Now we are ready for creating our own code. First thing that we will create is controller class. Name of controller will be ‘say’, to do this type next line in command prompt:<br />
<code><br />
ruby script/generate controller say<br />
</code><br />
5) After previous step there are new folders and files generated for us. We will first look for file holding our first controller, it is located in ‘\helloworld\app\controller\’ folder, and name of the file is ‘say_controller.rb’. Open this file in notepad and type next two lines of code after line with ‘class SayController &lt; ApplicationController’ and before line with ‘end’:<br />
<code><br />
def index<br />
end<br />
</code><br />
6) Now we will create new file, but now we will have to do it manually (no generate scripts for this one). We have to create <em>view</em> which will be related previous controller. To do this, navigate to folder ‘\helloworld\app\say\’ and create new file ‘index.html.erb’ with notepad, and type next line of code:<br />
<code><br />
&lt;h1&gt;Hello World from View&lt;/h1&gt;<br />
</code><br />
7) Believe it or not, we are ready to start our application. Because we are dealing with web application we will need some <em>server</em> that will accept calls from web browser. There is one easy solution for this, it is WEBrick server. To start WEBrick server, execute next line in command prompt:<br />
<code><br />
ruby script/server<br />
</code><br />
8) After previous step you will see some text in command prompt which describes what just happened. To see results in web browser, navigate to next url:</p>
<p><a href="http://localhost:3000/say">http://localhost:3000/say</a></p>
<p>9) You should see ‘Hello World’ message on the screen.</p>
<p>Now we will dissect previous steps, and talk a little bit more about each one of them.</p>
<p>Steps 1-3 are needed to create structure of rails application. By structure I mean nested folders inside application folder (in our case named ‘helloworld’). If you look inside application folder you will see plenty of folders. Each one of them is for particular purpose. For now we only used ‘app’ folder, but there are more which I will try to talk about next time.</p>
<p>We created rails application using just command prompt and notepad, but all this could be done inside some IDE like NetBeans, but for start its much better to do it this way using nothing more then command prompt, windows explorer and notepad.</p>
<p>Note that in step 3, we had to get inside application folder, which is because all ‘ruby’ prefixed commands in next steps will have results on application inside application folder.</p>
<p>In step 4, we created controller class by executing ‘ruby script/generate’ command and specifying what type of class we want to generate (controller) and its name (say). This as a results has creation of file ‘say_controller.rb’ inside ‘\app\controllers\’ folder, and also folder ‘\app\views\say’ (beside this two mentioned things there are few more files generated, but about them some next time). So we have some clue about how things are organized here. There is folder for keeping controller, views and models (little bit later) together, and it may sounds familiar to pattern named MVC (Model-View-Controller). Rails applications are organized in that way.</p>
<p>In step 5, we entered first lines of our code, and they defined a method inside controller class. So just to mention this one too, controller class is SayController, it is inside say_controller.rb file, and we generated it by using ‘say’ token, so you see convention on work. There will be more things like this, when convention acts in order to simplify things and speed up development. SayController is extending class ApplicationController, all controllers does that.</p>
<p>It might be strange that even we created empty ‘index’ method, application rendered correctly ‘Hello World’ message in browser, how is that? It is because of the view we created later. So view is inside ‘\app\views\say\’ folder, this way rails knows it is related to ‘say’ controller. Name of the view is ‘index.html.erb’, so once again we see some convention, method name was ‘index’ and it is how view is named too. There is also double extension ‘.html.erb’, the shortest explanation for this is that it is a mixture of html code and embedded ruby code (ERB). And because its not plain html file (its view template) it has this kind of extension.</p>
<p>The content of view file is ‘&lt;h1&gt;Hello World from View&lt;/h1&gt;’. Hmm, this one is very plain html code, but it’s just a hello world application, we will see soon how to mix html and ruby.</p>
<p>Step 7 fires up WEBrick server. It is integrated server that comes with rails and its very useful in development process. Note that after executing  ‘ruby script/server’, command prompt freezes. It is because server process is holding that command prompt blocked for new commands. Unless we use ‘ctrl+c’ key combination to stop server, this command prompt is useless. So it is the best to start with two command prompts, one for manipulating with application and other reserved for server. There are other servers too, one of them that is simple also is Mongrel.</p>
<p>When we have server ready for accepting requests we can open web browser and enter url ‘<a href="http://localhost:3000/say">http://localhost:3000/say</a>’. From this url we see that we are trying to connect on localhost machine (our machine) at the 3000 port number (its port on which WEBrick is working on), and we are requesting ‘say’. How this is translated: WEBrick receives request, delegates it to the rails engine which is parsing url and creating map of values. In this case there is just one token ‘say’ which rails will translate as a name of controller. So it will call something inside SayController, some method (or action in rails term), but we haven’t supplied any particular action in url, so by convention ‘index’ will be called and we created that one inside controller. ‘index’ method is executed and right after that rails search for view related to ‘index’ action and finds ‘index.html.erb’ which renders hello message. And that’s it, the easiest hello world rails application.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/railsonblues.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/railsonblues.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/railsonblues.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/railsonblues.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/railsonblues.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/railsonblues.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/railsonblues.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/railsonblues.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/railsonblues.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/railsonblues.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/railsonblues.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/railsonblues.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/railsonblues.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/railsonblues.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=railsonblues.wordpress.com&amp;blog=9570340&amp;post=3&amp;subd=railsonblues&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://railsonblues.wordpress.com/2009/10/05/hello-world-from-view/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ee36179a6041c88c313d9021b7a348f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">railsonblues</media:title>
		</media:content>
	</item>
	</channel>
</rss>
