<?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>Winn.ws &#187; Ruby on Rails</title>
	<atom:link href="http://winn.ws/archives/tag/ruby-on-rails/feed" rel="self" type="application/rss+xml" />
	<link>http://winn.ws</link>
	<description>Standards-based design &#38; development</description>
	<lastBuildDate>Thu, 09 Sep 2010 14:05:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using remote_function</title>
		<link>http://winn.ws/archives/171</link>
		<comments>http://winn.ws/archives/171#comments</comments>
		<pubDate>Mon, 20 Oct 2008 16:00:37 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/?p=171</guid>
		<description><![CDATA[This wonderful built-in function is a great way to make easy Ajax calls. In this example I will show you how to use remote_function with the onClick call, and also within a JavaScript function.
First, lets make a checkbox called finish; this link will send the id of the item to the database to be updated. [...]]]></description>
			<content:encoded><![CDATA[<p>This wonderful built-in function is a great way to make easy Ajax calls. In this example I will show you how to use remote_function with the onClick call, and also within a JavaScript function.</p>
<p>First, lets make a checkbox called finish; this link will send the id of the item to the database to be updated. The item will be a todo and we will be using the list.html.erb and the todo_controller.rb.</p>
<p>In the view named: list.html.erb</p>
<pre class="ruby">
<code>
remote_function(:url => {:action => 'finish', :controller => 'todo',
:id => todo.id})
</code>
</pre>
<p>Now once the checkbox is clicked it will them make the Ajax call to the todo_controller.rb with the method finish.</p>
<p>In the controller: todo_controller.rb</p>
<pre class="ruby">
<code>
def finish
	@todo = Todo.update(params[:id], :status => '1')
	if @todo.save
		render :update do |page|
		page.replace_html 'thedivID' + params[:id], 'Finished!'
		end
	end
end
</code>
</pre>
<p>The above code will first update the database setting the status from 0 to 1 (1 = finished and 0 = unfinished). Next we check if the update was successful, after that we render the update to the page. By using render :update, we can replace the html in side of the div or other element with the text ‘Finished’.</p>
<p><em>Note you can use remote_function in any way you see fit, ie: onclick, onload and others.</em></p>
<p>Passing another var with remote_function. lets say i want to pass the user id, list id and, the todo id. How would we do this with the remote function?</p>
<p>We know know the link should look like this: /todo/finish/17?todoid=72&#038;userid=100, but how do we do this with rails? Below is how to do just that;</p>
<pre class="ruby">
<code>
remote_function(:url => {:action => 'finish', :controller => 'todo',
:id => list.id, :todoid => todo.id, :userid => @user.id})
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/171/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Winn Scriptatype: 1.7</title>
		<link>http://winn.ws/archives/169</link>
		<comments>http://winn.ws/archives/169#comments</comments>
		<pubDate>Wed, 15 Oct 2008 02:17:58 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[WinnScriptatype]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/?p=169</guid>
		<description><![CDATA[I wanted to re-cap the new adds in 1.7 for rails. The new method calls are as listed:

# Added V1.7b
textlink_do_fade
textlink_do_puff
textlink_do_blindup
textlink_do_dropout

You can build any of the above calls with:

textlink_do_fade&#40;'Link Text','divYouWantToUpdate',&#123;***&#125;,&#123;****&#125;&#41;

Visit the project for more info on how to use the latest methods and more. Winn Scriptatype project home page. at Ruby On Rails Railway.
]]></description>
			<content:encoded><![CDATA[<p>I wanted to re-cap the new adds in 1.7 for rails. The new method calls are as listed:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Added V1.7b</span>
textlink_do_fade
textlink_do_puff
textlink_do_blindup
textlink_do_dropout</pre></div></div>

<p>You can build any of the above calls with:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">textlink_do_fade<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Link Text'</span>,<span style="color:#996600;">'divYouWantToUpdate'</span>,<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">***</span><span style="color:#006600; font-weight:bold;">&#125;</span>,<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">****</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Visit the project for more info on how to use the latest methods and more. <a href="http://rubyonrailsrailway.com/winnscriptatype">Winn Scriptatype project home page.</a> at <a href="http://rubyonrailsrailway.com/">Ruby On Rails Railway</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/169/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WinnScriptatype:Rails v1.7b</title>
		<link>http://winn.ws/archives/135</link>
		<comments>http://winn.ws/archives/135#comments</comments>
		<pubDate>Fri, 04 Apr 2008 03:18:55 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[WinnScriptatype]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[plug-in]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/135</guid>
		<description><![CDATA[I just released WinnScriptatype version 1.7 beta on the RubyForge project page. I have also made some fixes to version 1.5 and it is now production ready. The latest release has added some new functionality and the ability to make calls to display text. The call still supports the effects and Ajax request url along [...]]]></description>
			<content:encoded><![CDATA[<p>I just released WinnScriptatype version 1.7 beta on the RubyForge project page. I have also made some fixes to version 1.5 and it is now production ready. The latest release has added some new functionality and the ability to make calls to display text. The call still supports the effects and Ajax request url along with url options.</p>
<p>[ruby]<br />
# New calls in 1.7b<br />
textlink_do_fade<br />
textlink_do_puff<br />
textlink_do_blindup<br />
textlink_do_dropout<br />
[/ruby]</p>
<p>I expect i will be making the same update to the PHP version soon so keep an eye out for that!</p>
<p>Take a look at the <a href="http://rubyforge.org/projects/winnscriptatype/">WinnScriptatype:Rails RubyForge project page</a> and download the latest!</p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/135/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails Plugin: WinnScriptatype</title>
		<link>http://winn.ws/archives/129</link>
		<comments>http://winn.ws/archives/129#comments</comments>
		<pubDate>Thu, 27 Mar 2008 02:45:00 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[WinnScriptatype]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[plug-in]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/129</guid>
		<description><![CDATA[Making an Ajax checkbox just got easy. This very simple plugin was built for my own use. The idea was to make one call for my checkbox, update to the database, and an effect. This plugin is easy to use and install, i dont have it setup in an svn yet so you will need [...]]]></description>
			<content:encoded><![CDATA[<p>Making an Ajax checkbox just got easy. This very simple plugin was built for my own use. The idea was to make one call for my checkbox, update to the database, and an effect. This plugin is easy to use and install, i dont have it setup in an svn yet so you will need to download it and place it in RAILS_ROOT/vendor/plugins/HERE.</p>
<p>I had been working on this plugin for a few weeks in making a new application. I kept writing the &#8220;input, Ajax.Request and, Effect.Fade&#8221;, so i needed a way to make that fast and do what i need. And out came this handy plugin!</p>
<p>[ruby]<br />
# Lets use this new plugin<br />
checkbox_do_fade(&#8216;theDiv&#8217;, &#8216;CheckboxID&#8217;, {:controller => &#8216;todo&#8217;, :action => &#8216;finish&#8217;, :id => todo.id}, {:userid => user.id})</p>
<p>[/ruby]</p>
<h4><strong>Here is an over view of the options:</strong></h4>
<p>[ruby]<br />
checkbox_do_fade(&#8216;*&#8217;, &#8216;**&#8217;, {***}, {****})</p>
<p>[Element ID]* This is the name of the element you want to effect such as a div or table (tr,td)&#8230;</p>
<p>[Checkbox ID]** This is the id of the checkbox it self</p>
<p>[URL/path]*** this is the url for the update to take place NOTE: always use controller,<br />
              action, and id DO NOT place other variables in this block that is for the next block.</p>
<p>[URL/path options]**** This block is used for url options like so: (/path/to/1?userid=1) as you see<br />
                       userid is 1, this is done by using the url options block. You may send an unlimited number of<br />
                       url options with this.</p>
<p># Below are the ONLY supported calls<br />
checkbox_do_fade # This will fade the element<br />
checkbox_do_puff # This will puff the element<br />
checkbox_do_blindup # and so on&#8230;<br />
checkbox_do_dropout # again&#8230;<br />
[/ruby]</p>
<p>Ready to start using it? <a href="http://svn.winn.ws/winn_scriptatype.zip">Download WinnScriptatype</a></p>
<p>You will also need help installing this plugin so take a look at <a href="http://rubyonrailsrailway.com/winnscriptatype">Ruby on Rails Railway : WinnScriptatype</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/129/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Magic Multi-Connections</title>
		<link>http://winn.ws/archives/126</link>
		<comments>http://winn.ws/archives/126#comments</comments>
		<pubDate>Tue, 25 Mar 2008 14:00:55 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[plug-in]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/126</guid>
		<description><![CDATA[This plug-in is a great one! I have found a few bugs but it did the job just as i needed it to. I am using this plug-in to connect to three databases, where i am selecting, updating, inserting, and deleting. By adding this plug-in i was able to set my databases in the database.yml [...]]]></description>
			<content:encoded><![CDATA[<p>This plug-in is a great one! I have found a few bugs but it did the job just as i needed it to. I am using this plug-in to connect to three databases, where i am selecting, updating, inserting, and deleting. By adding this plug-in i was able to set my databases in the database.yml file and name them as i wanted. Next i adding two modules to my environment.rb file. After that i was able to connect to each of the databases with ease.</p>
<p>Below is how my database.yml file looks:</p>
<p>[ruby]<br />
databaseone:<br />
  adapter: mysql<br />
  database: one<br />
  username: root<br />
  password:<br />
  host: localhost</p>
<p>databasetwo:<br />
  adapter: mysql<br />
  database: two<br />
  username: root<br />
  password:<br />
  host: localhost</p>
<p>production:<br />
  adapter: mysql<br />
  database: three<br />
  username: root<br />
  password:<br />
  host: localhost<br />
[/ruby]</p>
<p>Now in my environment.rb file i have:</p>
<p>[ruby]<br />
module Dbone<br />
  establish_connection :databaseone<br />
end<br />
module Dbtwo<br />
  establish_connection :databasetwo<br />
end<br />
[/ruby]</p>
<p>Now if i would like to select from my database one i do the following in any controller:</p>
<p>[ruby]<br />
def index<br />
  @users = Dbone::Users.find(:all)<br />
end<br />
[/ruby]</p>
<p>Get the idea now? It&#8217;s the same to update and add to the database. Just use the module name. Now for the &#8220;production&#8221; database everything remains the same, no need to add a module to it.</p>
<p>Get <a href="http://magicmodels.rubyforge.org/magic_multi_connections/" target="_blank">Magic Multi-Connections V1</a></p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/126/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>visual_effect with a delay</title>
		<link>http://winn.ws/archives/122</link>
		<comments>http://winn.ws/archives/122#comments</comments>
		<pubDate>Sun, 02 Mar 2008 03:09:52 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Delay]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Visual Effect]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/122</guid>
		<description><![CDATA[One of the coolest things I ran across in RJS was the delay method. This delay method will do just that and delay a number of seconds. So lets say you need to highlight something that was just added to the page after the user submits it. I would like it to add to the [...]]]></description>
			<content:encoded><![CDATA[<p><img style="float:left; padding:1em; border:none;" src='http://www.winn.ws/wp-content/uploads/2008/02/2ndstepimage.png' alt='Ruby on Rails' />One of the coolest things I ran across in RJS was the delay method. This delay method will do just that and delay a number of seconds. So lets say you need to highlight something that was just added to the page after the user submits it. I would like it to add to the page then wait 1 second and then highlight. The following is the code for that.</p>
<p style="text-align:center;"><script type="text/javascript"><!--
google_ad_client = "pub-9763253940568418";
/* 468x15, created 5/28/10 */
google_ad_slot = "4528267187";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">render <span style="color:#ff3333; font-weight:bold;">:update</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page<span style="color:#006600; font-weight:bold;">|</span>
      page.<span style="color:#9900CC;">html_replace</span> <span style="color:#996600;">'testdiv'</span>, params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:the_text_added</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
      page.<span style="color:#9900CC;">delay</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
        page.<span style="color:#9900CC;">visual_effect</span> <span style="color:#ff3333; font-weight:bold;">:highlight</span>, <span style="color:#996600;">'testdiv'</span>, <span style="color:#ff3333; font-weight:bold;">:duration</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Cool huh? Try it out and see what you can do with delay()!</p>
<p>Need more help with rails? Here is a great book that i recommend for all new and old rails developers: <a href="http://www.amazon.com/gp/product/B0017UEQU2?ie=UTF8&#038;tag=winnws-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=B0017UEQU2">Beginning Ruby: From Novice to Professional</a><img src="http://www.assoc-amazon.com/e/ir?t=winnws-20&#038;l=as2&#038;o=1&#038;a=B0017UEQU2" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/122/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Highlight your UI with rails</title>
		<link>http://winn.ws/archives/119</link>
		<comments>http://winn.ws/archives/119#comments</comments>
		<pubDate>Wed, 20 Feb 2008 03:15:46 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/119</guid>
		<description><![CDATA[If you find yourself needing to highlight something after your Ajax call comes back and is finished. Why not highlight it? If your making a call with remote_function, link_to_remote or other then add this small line of code on. Problem solved!
[ruby]
:complete => visual_effect(:highlight, &#8216;theDivId&#8217;)
[/ruby]
Just give it the div or element id and it works like [...]]]></description>
			<content:encoded><![CDATA[<p>If you find yourself needing to highlight something after your Ajax call comes back and is finished. Why not highlight it? If your making a call with remote_function, link_to_remote or other then add this small line of code on. Problem solved!</p>
<p>[ruby]<br />
:complete => visual_effect(:highlight, &#8216;theDivId&#8217;)<br />
[/ruby]</p>
<p>Just give it the div or element id and it works like a charm!</p>
<p>Don&#8217;t know how to use remote_function? Check out this post: <a href="http://rubyonrailsrailway.com/2008/2/20/using-remote_function">Using remote_function</a></p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/119/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Listing files in a directory with Rails</title>
		<link>http://winn.ws/archives/118</link>
		<comments>http://winn.ws/archives/118#comments</comments>
		<pubDate>Mon, 18 Feb 2008 02:17:05 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Files]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Showing files]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/118</guid>
		<description><![CDATA[
In a recent application I am building I needed to pull file names and directory names from a local directory. I searched all over the net for this and found nothing. After reviewing a few of rails built in functions I came across Dir.glob(). If you feed this method a path to a directory it [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.winn.ws/wp-content/uploads/2008/02/2ndstepimage.png" style="float: left; margin:.5em;" alt="Ruby on Rails" /><br />
In a recent application I am building I needed to pull file names and directory names from a local directory. I searched all over the net for this and found nothing. After reviewing a few of rails built in functions I came across Dir.glob(). If you feed this method a path to a directory it will then come back with an array of the names and directories listed.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-9763253940568418";
/* 468x15, created 5/28/10 */
google_ad_slot = "4528267187";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>So lets say I am going to tell Dir.glob to look in my &#8220;files&#8221; folder. The path I need to feed it is starting from the root of my app so the path would be &#8220;public/files/&#8221;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@files</span> = <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">glob</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;public/files/*&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>I added &#8220;*&#8221; so I can pull all files and dir’s back. If I just want to pull PDF’s back I can use this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@files</span> = <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">glob</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;public/files/*.pdf&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>That will show all PDF’s in that folder. You may also give it a file name and just pull one file out.</p>
<p>Anyway back to example one, I am looking for all files and all directories in the folder &#8220;files&#8221;. Below is the array I get back from @files.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">for</span> file <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#0066ff; font-weight:bold;">@files</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> file
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;">#====</span>
Output is:
&nbsp;
public<span style="color:#006600; font-weight:bold;">/</span>files<span style="color:#006600; font-weight:bold;">/</span>test1.<span style="color:#9900CC;">gif</span>
public<span style="color:#006600; font-weight:bold;">/</span>files<span style="color:#006600; font-weight:bold;">/</span>test2.<span style="color:#9900CC;">gif</span>
public<span style="color:#006600; font-weight:bold;">/</span>files<span style="color:#006600; font-weight:bold;">/</span>folder</pre></td></tr></table></div>

<p>This is the best way I have found for pulling files from a directory and listing them in an array.</p>
<p>Need more help with rails? Here is a great book that i recommend for all new and old rails developers: <a href="http://www.amazon.com/gp/product/B0017UEQU2?ie=UTF8&#038;tag=winnws-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=B0017UEQU2">Beginning Ruby: From Novice to Professional</a><img src="http://www.assoc-amazon.com/e/ir?t=winnws-20&#038;l=as2&#038;o=1&#038;a=B0017UEQU2" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/118/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to download files with a Ruby</title>
		<link>http://winn.ws/archives/116</link>
		<comments>http://winn.ws/archives/116#comments</comments>
		<pubDate>Sun, 17 Feb 2008 03:51:33 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/116</guid>
		<description><![CDATA[For a project that I&#8217;m working on, I want to download a set of pictures from flickr once a day. In this way, my Rails app won&#8217;t need to access the flickr api &#8211; it can just grab the pics out of an images directory.
I figured that it would be an awfully easy task to [...]]]></description>
			<content:encoded><![CDATA[<p>For a project that I&#8217;m working on, I want to download a set of pictures from flickr once a day. In this way, my Rails app won&#8217;t need to access the flickr api &#8211; it can just grab the pics out of an images directory.</p>
<p>I figured that it would be an awfully easy task to download pictures from the internet using Ruby, and it is, but it was very difficult to find good examples. I googled for an hour or so, and kept coming up empty-handed. Finally, I checked in gmail where I keep mail from the Ruby Language mailing list. I found a relatively straightfoward way to do this.</p>
<p>Let&#8217;s say that you want to download a picture from flickr, and you know its URL. Here&#8217;s the ruby script to download the file:</p>
<p>[ruby]<br />
require &#8216;net/http&#8217;<br />
Net::HTTP.start(&#8220;static.flickr.com&#8221;) { |http|<br />
resp = http.get(&#8220;/92/218926700_ecedc5fef7_o.jpg&#8221;)<br />
open(&#8220;fun.jpg&#8221;, &#8220;wb&#8221;) { |file|<br />
file.write(resp.body)<br />
}<br />
}<br />
[/ruby]</p>
<p><span id="more-116"></span></p>
<p>The Net::HTTP class contains the magic needed to handle this operation. I don&#8217;t think this will work at all if you need to pull down a file from an FTP server. For now, we&#8217;re dealing with http urls. So, just strip off the &#8220;http://&#8221; portion of the url and everything after that up to the first / goes into the start method.</p>
<p>Now, we get the file. The rest of your image url after what you put in the start method goes into the get method. This grabs the file from flickr.</p>
<p>Now we&#8217;re going to copy the file down to where the script is running. First, let&#8217;s create the file we&#8217;re going to copy the picture into. Using the open method, the first parameter is the name of the file that you&#8217;re going to plop the picture into. We could have used &#8220;218926700_ecedc5fef7_o.jpg&#8221; or anything else here. The second parameter of the open method, &#8220;wb&#8221; indicates that we&#8217;re opening the file for (w)riting and we&#8217;re going to be writing (b)inary information. The &#8220;b&#8221; may not be necessary on non-Windows platforms.</p>
<p>Finally, we&#8217;re going to write into the new file, the contents or &#8220;body&#8221; of what we grabbed from flickr. So, this writes in the binary bits of the picture into fun.jpg. Remember that with the way that we created fun.jpg it&#8217;ll be in the same directory with our ruby script.</p>
<p>This same method will copy down .html files, .css files, .pdf&#8217;s and just about any other kind of file. In my next RubyNoob entry, I&#8217;ll write about how to combine this method with a flickr api call to grab an arbitrary number of recent photos from flickr. As usual, I&#8217;m still a noob, and there&#8217;s probably much better ways to do this. If you know a better way, please share in the comment section below.</p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/116/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Practical security on your application</title>
		<link>http://winn.ws/archives/109</link>
		<comments>http://winn.ws/archives/109#comments</comments>
		<pubDate>Sun, 06 Jan 2008 00:57:01 +0000</pubDate>
		<dc:creator>Greg Winn</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[user authentication]]></category>

		<guid isPermaLink="false">http://www.winn.ws/archives/109</guid>
		<description><![CDATA[Developing online applications is a way of life for a lot of programmers! It’s a great feeling working your but off day in and day out, night in and night out, sometimes all night and sometimes with no brakes! Then launch day comes and you are so happy any cant wait for people to start [...]]]></description>
			<content:encoded><![CDATA[<p>Developing online applications is a way of life for a lot of programmers! It’s a great feeling working your but off day in and day out, night in and night out, sometimes all night and sometimes with no brakes! Then launch day comes and you are so happy any cant wait for people to start using your application! You launch your application without a hitch, all goes well!</p>
<p>Keeping that application secure is important, so when do you start worrying about security? I say right away, start building your application with maximum security. Keep track of what cookies you are setting as they can be in danger from a JavaScript injection.</p>
<p>So if your application has to use cookies how should you authenticate the user? I find writing a session id to the database and to a cookie along with a username or email, this process becomes very easy and secure. So how does this work?</p>
<p>Start with a login or registration, and then start the session. Pull the session id into a variable the send it to the database. After that write the session id to a cookie.</p>
<p>[php]<br />
session_start();<br />
$sid = session_id();</p>
<p>// your SQL insert or update &#8220;UPDATE accounts SET sid=&#8217;$sid&#8217; WHERE id=1&#8243;</p>
<p>setcookie(&#8216;appname_session&#8217;, $sid, time()+3600);<br />
[/php]</p>
<p>After setting the session id cookie we need to set the username or email cookie!</p>
<p>[php]<br />
setcookie(&#8216;appname_user&#8217;, $username, time()+3600);<br />
[/php]</p>
<p>Now you are able to authenticate the user inside your application securely.</p>
<p>[php]<br />
// Select the user now<br />
find_by_username_and_sid(cookie(&#8220;appname_user&#8221;),cookie(&#8220;appname_session&#8221;));<br />
[/php]</p>
<p><strong>This is in PHP but the same idea applies in all languages.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://winn.ws/archives/109/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 2/10 queries in 0.002 seconds using memcached
Content Delivery Network via Amazon Web Services: CloudFront: dhbbuddo7esu2.cloudfront.net

Served from: winn.ws @ 2010-09-09 22:26:46 -->