<?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>Building Browsergames &#187; rubyonrails</title>
	<atom:link href="http://buildingbrowsergames.com/tag/rubyonrails/feed/" rel="self" type="application/rss+xml" />
	<link>http://buildingbrowsergames.com</link>
	<description>Ever wanted to build a browsergame?</description>
	<lastBuildDate>Mon, 29 Mar 2010 14:00:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Forcing Users To Log In (Ruby on Rails)</title>
		<link>http://buildingbrowsergames.com/2008/10/06/forcing-users-to-log-in-ruby-on-rails/</link>
		<comments>http://buildingbrowsergames.com/2008/10/06/forcing-users-to-log-in-ruby-on-rails/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 14:00:02 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[medieval]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=449</guid>
		<description><![CDATA[Although I demonstrated protecting a page from access by a user who wasn&#8217;t logged in back when we did the Forest controller for combat, I didn&#8217;t protect either the Bank or Healer pages when we did them. It&#8217;s easy enough to commit that kind of mistake now, but ten times easier when your pages are [...]]]></description>
			<content:encoded><![CDATA[<p>Although I demonstrated protecting a page from access by a user who wasn&#8217;t logged in back when we did the Forest controller for combat, I didn&#8217;t protect either the Bank or Healer pages when we did them. It&#8217;s easy enough to commit that kind of mistake now, but ten times easier when your pages are much more complicated and you&#8217;re trying to get your game finished. In this entry we&#8217;ll not only fix both of the pages we missed, we&#8217;ll set it up so all the future pages we add are protected automatically from access by a user who isn&#8217;t logged in.</p>
<p>The way that occurs to you first is to fix all the controllers individually. That is, add the &#8220;before_filter&#8230;&#8221; line to bank_controller.rb, healer_controller.rb, etc. But that has two problems with it. One is that it is repeating ourselves and Rails is always trying to teach you &#8220;Don&#8217;t Repeat Yourself&#8221; and the other problem is that it is error prone. It&#8217;s too easy to add a new controller for some new pages and forget to put security on them.</p>
<p>What we want in all cases is for security to already be there and we just turn it off in those few cases where we <em>don&#8217;t need it</em> rather than having to remember to do so when we do need it.</p>
<p>So we&#8217;ll remove the &#8220;before_filter :login_required&#8221; line from the forest_controller.rb and move it instead to the application.rb file. Since all our other controllers inherit from this one controller, every method on all of them will be instantly protected. In fact, we&#8217;re now a little <em>too</em> protected, the user can no longer go to even the welcome page without logging in first.</p>
<p>In order to fix that, we&#8217;ll add an override to three controllers. The Welcome controller (welcome_controller.rb), the Users controller (users_controller.rb), and the Sessions controller (sessions_controller.rb):</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">skip_before_filter <span style="color:#ff3333; font-weight:bold;">:login_required</span></pre></div></div>

<p>With that in place we&#8217;ll skip the login requirement just for the pages related to those three controllers but the bank, healer, forest, <strong>and any other controller we add from now on</strong> will be protected from entry by users who haven&#8217;t logged in yet.</p>
<h2>Authorization Is Not Authentication</h2>
<p>I almost made this an extra credit item but it became too long and it&#8217;s a basic thing that most any game is going to have to deal with eventually. Authentication and authorization are not the same thing. All we have gotten so far from restful_authentication is just that, authentication. It lets someone sign up in the system and then verifies later via login name and password that the person trying to log in is a person the site has seen before and specifically which one it is.</p>
<p>Authorization is about permission to do things. Once I know who logged in, what is he or she allowed to do? Play the game, kick out duplicate accounts, end the game? restful_authentication has hooks built in which can be used with other plugins to manage <a href="http://www.writertopia.com/developers/authorization">complicated permission systems</a> but you can also easily stick some simple testing in yourself. Let&#8217;s add a page which can only be accessed by a select user to demonstrate this.</p>
<pre>
> ruby script/generate controller Admin index
</pre>
<p>Update your new Admin controller (app/controllers/admin_controller.rb) to add the authorized? function you see below:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> AdminController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> index
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private
&nbsp;
  <span style="color:#008000; font-style:italic;"># By adding a function named &quot;authorized?&quot; and performing a test in it</span>
  <span style="color:#008000; font-style:italic;"># we use one of the hooks provided by restful_authentication.</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> authorized?
    current_user.<span style="color:#9900CC;">login</span> == <span style="color:#996600;">&quot;Admin&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>  
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now try to go to the admin page (http://localhost:3000/admin). If you create a user named &#8220;Admin&#8221; then you will be able to access the page when you are that user. Any other user will fail and will be automatically redirected.</p>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/10/06/forcing-users-to-log-in-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Healing Your Players (Ruby on Rails)</title>
		<link>http://buildingbrowsergames.com/2008/10/03/healing-your-players-ruby-on-rails/</link>
		<comments>http://buildingbrowsergames.com/2008/10/03/healing-your-players-ruby-on-rails/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 14:00:49 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[medieval]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=437</guid>
		<description><![CDATA[If you went through the entry on building the bank then this is essentially the same thing again but the code is actually slightly simpler. As before it starts off with generating a controller and an index page to handle the user interface: Healing Your Players (PHP)

> ruby script/generate controller Healer index

We add a new [...]]]></description>
			<content:encoded><![CDATA[<p>If you went through the entry on building the bank then this is essentially the same thing again but the code is actually slightly simpler. As before it starts off with generating a controller and an index page to handle the user interface: <a href="http://buildingbrowsergames.com/2008/06/17/building-browsergames-healing-your-players-php/">Healing Your Players (PHP)</a></p>
<pre>
> ruby script/generate controller Healer index
</pre>
<p>We add a new healing function to the User model (app/models/user.rb):</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> heal<span style="color:#006600; font-weight:bold;">&#40;</span>amount<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>amount <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">or</span> amount <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">gold</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    amount = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">gold</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>amount <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">max_hp</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">cur_hp</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    amount = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">max_hp</span> <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">cur_hp</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">gold</span> <span style="color:#006600; font-weight:bold;">-</span>= amount
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">cur_hp</span> <span style="color:#006600; font-weight:bold;">+</span>= amount
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#5A0A0A; font-weight:bold;">save</span>
&nbsp;
  amount
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The contents of app/views/healer/index.html.erb:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Welcome to the healer. You currently have 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;&lt;%<span style="color: #66cc66;">=</span> current_user.cur_hp %&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span> HP out of a maximum of 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;&lt;%<span style="color: #66cc66;">=</span> current_user.max_hp %&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>You have <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;&lt;%<span style="color: #66cc66;">=</span> current_user.gold%&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span> gold to heal yourself with, 
and it will cost you <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>1 gold per HP healed<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span> to heal yourself.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;% form_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/healer/do_some_healing'</span>, :<span style="color: #000066;">method</span> <span style="color: #66cc66;">=</span>&gt;</span> :post) do %&gt;
  <span style="color: #009900;">&lt;%<span style="color: #66cc66;">=</span> text_field_tag <span style="color: #ff0000;">'amount'</span> %&gt;</span>
  <span style="color: #009900;">&lt;%<span style="color: #66cc66;">=</span> submit_tag <span style="color: #ff0000;">'Heal Me'</span>%&gt;</span>
<span style="color: #009900;">&lt;% end %&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;%<span style="color: #66cc66;">=</span> link_to <span style="color: #ff0000;">&quot;Home&quot;</span>, :controller <span style="color: #66cc66;">=</span>&gt;</span> 'welcome' %&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
  window.onload = function() {
    document.getElementById('amount').focus();
  }
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>And, as always, we pull together our model changes and the view in the controller (app/controllers/healer_controller.rb):</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> HealerController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> index
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> do_some_healing
    <span style="color:#008000; font-style:italic;"># The amount we actually healed might be different than the amount requested</span>
    <span style="color:#008000; font-style:italic;"># due to a variety of factors (i.e. they didn't need that much healing, they</span>
    <span style="color:#008000; font-style:italic;"># didn't have enough gold, etc.) so we record the amount they actually healed</span>
    <span style="color:#008000; font-style:italic;"># which comes back from the method call.</span>
    amount = current_user.<span style="color:#9900CC;">heal</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:amount</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#5A0A0A; font-weight:bold;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;You have been healed for #{amount} HP.&quot;</span>
    <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;index&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>As with the bank and the forest, our finishing touch is to add a link to the destination on the welcome page (app/views/welcome/index.html.erb):</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;%<span style="color: #66cc66;">=</span> link_to <span style="color: #ff0000;">&quot;The Healer&quot;</span>, :controller <span style="color: #66cc66;">=</span>&gt;</span> &quot;healer&quot; %&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/10/03/healing-your-players-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating The Bank (Ruby on Rails)</title>
		<link>http://buildingbrowsergames.com/2008/10/02/creating-the-bank-ruby-on-rails/</link>
		<comments>http://buildingbrowsergames.com/2008/10/02/creating-the-bank-ruby-on-rails/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 14:00:48 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[buildingbrowsergames]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[medieval]]></category>
		<category><![CDATA[rubyonrails]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://buildingbrowsergames.com/?p=432</guid>
		<description><![CDATA[As always, it pays to quickly scan the original PHP version of this tutorial entry. That entry is available here: Creating The Bank (PHP)
In order to add a bank to the game we know we need a new page to handle the user interaction. So, does that go into an existing controller? At present I [...]]]></description>
			<content:encoded><![CDATA[<p>As always, it pays to quickly scan the original PHP version of this tutorial entry. That entry is available here: <a href="http://buildingbrowsergames.com/2008/06/13/building-browsergames-creating-the-bank-php/">Creating The Bank (PHP)</a></p>
<p>In order to add a bank to the game we know we need a new page to handle the user interaction. So, does that go into an existing controller? At present I think the answer is no, we&#8217;ll generate a new controller just for the bank (like we did for the forest):</p>
<pre>
> ruby script/generate controller Bank index
</pre>
<p>In addition to something to handle the UI, we also need someplace to keep track of the money in the bank so we&#8217;ll add a new field for that to the user:</p>
<pre>
> ruby script/generate migration AddBankgcToUser bankgc:integer
</pre>
<p>The migration we just generated will handle adding the new field to all existing users and any new ones that join, but we need to edit it to add a default value of zero for the field:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">add_column <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:bankgc</span>, <span style="color:#ff3333; font-weight:bold;">:integer</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span></pre></div></div>

<p>Be sure to run the migration to change the database table:</p>
<pre>
> rake db:migrate
</pre>
<p>Now we need some functionality to let us deposit money into the &#8220;bank&#8221; and withdraw it again later. To do that we&#8217;ll add two new functions into our User model (app/models/user.rb):</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> deposit<span style="color:#006600; font-weight:bold;">&#40;</span>amount<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>amount <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">or</span> amount <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">gold</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    amount = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">gold</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">bankgc</span> <span style="color:#006600; font-weight:bold;">+</span>= amount
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">gold</span> <span style="color:#006600; font-weight:bold;">-</span>= amount
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#5A0A0A; font-weight:bold;">save</span>
&nbsp;
  amount
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> withdraw<span style="color:#006600; font-weight:bold;">&#40;</span>amount<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>amount <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006666;">0</span> <span style="color:#9966CC; font-weight:bold;">or</span> amount <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">bankgc</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    amount = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">bankgc</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">bankgc</span> <span style="color:#006600; font-weight:bold;">-</span>= amount
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">gold</span> <span style="color:#006600; font-weight:bold;">+</span>= amount
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#5A0A0A; font-weight:bold;">save</span>
&nbsp;
  amount
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>One of the only things new and worth noting here is that the revised amount is referenced on the last line of each function. In Ruby that makes it a return value so we can see exactly how much really was moved when the user specifies a ridiculous value (i.e. attempting to withdraw 20000 with only 5 in the bank). We&#8217;ll use that in the controller code later.</p>
<p>Next we need to fill in the view. It will both inform the user of how much money he/she has in the bank and in hand and take input via a form and buttons to indicate which way the transfer should occur. Here&#8217;s the code for the view (app/views/bank/index.html.erb):</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;</span>Welcome to the bank. You currently have 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;&lt;%<span style="color: #66cc66;">=</span> @current_user.bankgc %&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span> gold in the bank, 
and <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;&lt;%<span style="color: #66cc66;">=</span> @current_user.gold %&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span> gold in hand.<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;% form_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/bank/do_some_banking'</span>, :<span style="color: #000066;">method</span> <span style="color: #66cc66;">=</span>&gt;</span> :post) do %&gt;
  <span style="color: #009900;">&lt;%<span style="color: #66cc66;">=</span> text_field_tag <span style="color: #ff0000;">'amount'</span> %&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span><span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;%<span style="color: #66cc66;">=</span> submit_tag <span style="color: #ff0000;">'Deposit'</span> %&gt;&lt;%<span style="color: #66cc66;">=</span> submit_tag <span style="color: #ff0000;">'Withdraw'</span> %&gt;</span>
<span style="color: #009900;">&lt;% end %&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span>&gt;&lt;%<span style="color: #66cc66;">=</span> link_to <span style="color: #ff0000;">'Home'</span>, :controller <span style="color: #66cc66;">=</span>&gt;</span> 'welcome' %&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
  window.onload = function() {
    document.getElementById('amount').focus();
  }
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>One thing slightly different about this form is that we have one form with multiple different submit buttons. We can differentiate between them when the controller gets the submitted results of the form so we know what to do. Another thing to note about this form is that it isn&#8217;t a form_for, but instead a form_tag. form_for makes it easy to have a set of fields and populate them from a model object, then gather up the results and put them into a hash which can be assigned directly back to the same type of model object to create a new one or edit the values in it. But we don&#8217;t have a model for this page, we just one one value (a number in this case) returned to the controller so form_tag is what makes sense.</p>
<p>Lastly we will pull all these pieces together in the controller. We&#8217;ll use the hash of parameters created from the user filling in the form and figure out which of the new methods on the user model to call. As before, the model is doing the real work, the view is taking the user&#8217;s input, and the controller is gluing the two together. Here&#8217;s the code for the Bank controller we generated earlier (app/controllers/bank_controller.rb):</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> BankController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> index
    current_user
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> do_some_banking
    <span style="color:#9966CC; font-weight:bold;">if</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:commit</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">&quot;Deposit&quot;</span>
      amount = current_user.<span style="color:#9900CC;">deposit</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:amount</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#5A0A0A; font-weight:bold;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;You deposited #{amount} gold into your bank account. Your total in the bank is now #{@current_user.bankgc}.&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      amount = current_user.<span style="color:#9900CC;">withdraw</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:amount</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#5A0A0A; font-weight:bold;">to_i</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      <span style="color:#5A0A0A; font-weight:bold;">flash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;You withdraw #{amount} gold from your bank account. Your total gold in hand is now #{@current_user.gold}.&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;index&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The code consists of getting the current user and then performing actions on that user. The code we added to the model gives us the new features we need and the view got the values into the hash so we can find them. The controller is only responsible for getting the user&#8217;s input out of the hash and passing it on to the correct functions in the model. It also acts as a translator to take responses from the model and turn them into messages which can be displayed by the view. As I mentioned before, we can look in the hash coming from the view and the value of the params[:commit] tells the code whether the user clicked on the &#8220;Deposit&#8221; or &#8220;Withdraw&#8221; button.</p>
<p>Our final touch is adding the bank as a destination to the welcome page (for a logged in user). Add this below the corresponding link to the forest in app/views/welcome/index.html.erb:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;%<span style="color: #66cc66;">=</span> link_to <span style="color: #ff0000;">&quot;The Bank&quot;</span>, :controller <span style="color: #66cc66;">=</span>&gt;</span> &quot;bank&quot; %&gt;</pre></div></div>

<h2>Extra Credit</h2>
<ol>
<li>&#8220;rake &#8211;tasks&#8221; tells you about all the commands rake supports. We&#8217;ve used &#8220;rake db:migrate&#8221; many times but there are actually several rake functions to manipulate the database. In particular I&#8217;ll point out &#8220;rake db:migrate:redo&#8221;. I used it while working on this particular entry because the first time I built it I forgot to set a default value for bankgc in the user, so I went back and added the default value and used the &#8220;rake db:migrate:redo&#8221; to undo the last migration (it calls the self.down in the migration) and then run the migration again to correct the error. Then I had the new field on each user and it had a default value of zero for all of them.</li>
<li><a href="http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for">form_for</a> and <a href="http://apidock.com/rails/ActionView/Helpers/FormTagHelper/form_tag">form_tag</a> are both helper functions. Helper functions are Rails methods designed to help you generate HTML with less pain, and in the case of form_for, generate HTML that follows certain patterns (e.g. the naming of fields in the form) so that it will work together with code to make some activity simpler.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://buildingbrowsergames.com/2008/10/02/creating-the-bank-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

