<?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>ruby | Spiffy Stores Blog</title>
	<atom:link href="https://www.spiffystores.com.au/blog/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.spiffystores.com.au/blog</link>
	<description>Checkout - The Spiffy Stores Blog</description>
	<lastBuildDate>Tue, 31 Aug 2021 21:17:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
<site xmlns="com-wordpress:feed-additions:1">241205771</site>	<item>
		<title>Problems rendering a layout in Rails3</title>
		<link>https://www.spiffystores.com.au/blog/2013/01/04/problems-rendering-a-layout-in-rails3/</link>
		
		<dc:creator><![CDATA[Brian]]></dc:creator>
		<pubDate>Fri, 04 Jan 2013 04:49:43 +0000</pubDate>
				<category><![CDATA[Geek stuff]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[initialize]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails 3]]></category>
		<category><![CDATA[rails3]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[super]]></category>
		<guid isPermaLink="false">https://www.spiffystores.com.au/blog/?p=1883</guid>

					<description><![CDATA[<p>From time to time we like to share technical tips when we&#8217;ve uncovered a solution to a problem that might help other Rails developers. Spiffy Stores is written using the Ruby on Rails framework, and we encountered a glitch with &#8230; <a href="https://www.spiffystores.com.au/blog/2013/01/04/problems-rendering-a-layout-in-rails3/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
The post <a href="https://www.spiffystores.com.au/blog/2013/01/04/problems-rendering-a-layout-in-rails3/">Problems rendering a layout in Rails3</a> first appeared on <a href="https://www.spiffystores.com.au/blog">Spiffy Stores Blog</a>.]]></description>
										<content:encoded><![CDATA[<div style='display:none;' class='shareaholic-canvas' data-app='share_buttons' data-title='Problems rendering a layout in Rails3' data-link='https://www.spiffystores.com.au/blog/2013/01/04/problems-rendering-a-layout-in-rails3/' data-app-id-name='category_above_content'></div>
<p>From time to time we like to share technical tips when we&#8217;ve uncovered a solution to a problem that might help other Rails developers.</p>



<p>Spiffy Stores is written using the Ruby on Rails framework, and we encountered a glitch with the Rails3 layouts. Basically we couldn&#8217;t get the layout to display, even though all the syntax was correct. Others have experienced this sort of problem. See <a href="http://stackoverflow.com/questions/6605716/cant-render-layout-in-rails-3" target="_blank" rel="noopener">http://stackoverflow.com/questions/6605716/cant-render-layout-in-rails-3</a> for an example.</p>



<p>After lots of digging around and tracing, the answer became clear. The AbstractController::Layouts module has an initialize method, but this method was not being called when a new controller was created.</p>



<p>If you experience this problem, then check any modules that you have included in your controller, as one of them has an initialize method that doesn&#8217;t call &#8216;super&#8217;.</p>



<p>If an included module needs an initialize method, then it needs to follow this pattern:</p>



<pre class="wp-block-code"><code lang="ruby" class="language-ruby">def initialize(*)
  # Module initialization code here
  super
end</code></pre>



<p>If the call to &#8216;super&#8217; isn&#8217;t included, then the initialization chain stops, and your controller won&#8217;t be properly initialized. You can find out all the included modules for a controller by executing this code from the console:</p>



<pre class="wp-block-code"><code lang="ruby" class="language-ruby">MyController.ancestors</code></pre>
<div style='display:none;' class='shareaholic-canvas' data-app='share_buttons' data-title='Problems rendering a layout in Rails3' data-link='https://www.spiffystores.com.au/blog/2013/01/04/problems-rendering-a-layout-in-rails3/' data-app-id-name='category_below_content'></div><div style='display:none;' class='shareaholic-canvas' data-app='recommendations' data-title='Problems rendering a layout in Rails3' data-link='https://www.spiffystores.com.au/blog/2013/01/04/problems-rendering-a-layout-in-rails3/' data-app-id-name='category_below_content'></div>The post <a href="https://www.spiffystores.com.au/blog/2013/01/04/problems-rendering-a-layout-in-rails3/">Problems rendering a layout in Rails3</a> first appeared on <a href="https://www.spiffystores.com.au/blog">Spiffy Stores Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1883</post-id>	</item>
		<item>
		<title>Rails 2.2.2 memoization gotchas</title>
		<link>https://www.spiffystores.com.au/blog/2008/12/17/rails-222-memoization-gotchas/</link>
		
		<dc:creator><![CDATA[Brian]]></dc:creator>
		<pubDate>Wed, 17 Dec 2008 07:33:28 +0000</pubDate>
				<category><![CDATA[Geek stuff]]></category>
		<category><![CDATA[Show All]]></category>
		<category><![CDATA[1.3]]></category>
		<category><![CDATA[2.2.2]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[apache 1.3]]></category>
		<category><![CDATA[memoize]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rails 2.2.2]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ssl]]></category>
		<guid isPermaLink="false">http://spiffystores.com.au/blog/?p=147</guid>

					<description><![CDATA[<p>For various reasons, we are still running on Apache 1.3 which is fine, except for the fact that there is no easy way of determining whether a request is via an SSL session or not. Unfortunately, this process was broken &#8230; <a href="https://www.spiffystores.com.au/blog/2008/12/17/rails-222-memoization-gotchas/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
The post <a href="https://www.spiffystores.com.au/blog/2008/12/17/rails-222-memoization-gotchas/">Rails 2.2.2 memoization gotchas</a> first appeared on <a href="https://www.spiffystores.com.au/blog">Spiffy Stores Blog</a>.]]></description>
										<content:encoded><![CDATA[<div style='display:none;' class='shareaholic-canvas' data-app='share_buttons' data-title='Rails 2.2.2 memoization gotchas' data-link='https://www.spiffystores.com.au/blog/2008/12/17/rails-222-memoization-gotchas/' data-app-id-name='category_above_content'></div>
<p>For various reasons, we are still running on Apache 1.3 which is fine, except for the fact that there is no easy way of determining whether a request is via an SSL session or not.</p>



<p>Unfortunately, this process was broken by the upgrade to Rails 2.2.2.</p>



<span id="more-147"></span>



<p>No probs. We just rewrite the URL to add an &#8220;ssl&#8221; prefix before it gets passed to the Rails engine.</p>



<pre class="wp-block-code"><code lang="apacheconf" class="language-apacheconf">RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ http://127.0.0.1:8000/ssl/$1 [P,L]
ProxyPassReverse / http://127.0.0.1:8000/ssl/</code></pre>



<p>From there, a simple route will pass the request on to our SSL Proxy controller.</p>



<pre class="wp-block-code"><code lang="ruby" class="language-ruby">map.connect 'ssl/*suburi',
            :controller => 'ssl_proxy',
            :action => 'reprocess'</code></pre>



<p>Now all our &#8216;reprocess&#8217; method has to do is take the request, rewrite the URI to its original form, and set an environment variable to let us know it&#8217;s an SSL request.</p>



<pre class="wp-block-code"><code lang="ruby" class="language-ruby line-numbers">class SslProxyController &lt; ActionController::Base
  def reprocess
    request.env["REQUEST_URI"] = "/#{params[:suburi].join('/')}"
    request.env["HTTPS"] = "on"

    controller = ActionController::Routing::Routes.recognize(request)
    controller.process(request, response)
    @performed_render = true
  end
end</code></pre>



<p>This all broke down with the upgrade to Rails 2.2.2.</p>



<p>One of the new features of 2.2.2 is the addition of memoization to various system classes, including the &#8216;request&#8217; object. This means that &#8216;request_uri&#8217; is now memoized, and that even if we update the environment variable &#8216;REQUEST_URI&#8217;, the memoized value doesn&#8217;t change. When the route is recognized, it resolves to &#8216;reprocess&#8217; again, and we end up in an endless loop.</p>



<p>The answer turns to reasonably easy to implement. All we need to do is add the following line at the start of the &#8216;reprocess&#8217; method.</p>



<pre class="wp-block-code"><code lang="ruby" class="language-ruby">request.unmemoize_all if request.respond_to? :unmemoize_all</code></pre>



<p>This code simply clears all the memoized variables, so they can all be loaded again using our new, proper URI.</p>
<div style='display:none;' class='shareaholic-canvas' data-app='share_buttons' data-title='Rails 2.2.2 memoization gotchas' data-link='https://www.spiffystores.com.au/blog/2008/12/17/rails-222-memoization-gotchas/' data-app-id-name='category_below_content'></div><div style='display:none;' class='shareaholic-canvas' data-app='recommendations' data-title='Rails 2.2.2 memoization gotchas' data-link='https://www.spiffystores.com.au/blog/2008/12/17/rails-222-memoization-gotchas/' data-app-id-name='category_below_content'></div>The post <a href="https://www.spiffystores.com.au/blog/2008/12/17/rails-222-memoization-gotchas/">Rails 2.2.2 memoization gotchas</a> first appeared on <a href="https://www.spiffystores.com.au/blog">Spiffy Stores Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">147</post-id>	</item>
		<item>
		<title>How to Fake an Uploaded File</title>
		<link>https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/</link>
		
		<dc:creator><![CDATA[Brian]]></dc:creator>
		<pubDate>Wed, 05 Nov 2008 23:40:51 +0000</pubDate>
				<category><![CDATA[Geek stuff]]></category>
		<category><![CDATA[Show All]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[uploaded]]></category>
		<category><![CDATA[uploaded tempfile]]></category>
		<guid isPermaLink="false">http://spiffystores.com.au/blog/?p=74</guid>

					<description><![CDATA[<p>Our store software contains an extensive set of routines for processing uploaded images and resizing them into various image sizes. We&#8217;ve recently been adding some code to support a bulk import function and it&#8217;s become necessary to somehow fake uploading &#8230; <a href="https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
The post <a href="https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/">How to Fake an Uploaded File</a> first appeared on <a href="https://www.spiffystores.com.au/blog">Spiffy Stores Blog</a>.]]></description>
										<content:encoded><![CDATA[<div style='display:none;' class='shareaholic-canvas' data-app='share_buttons' data-title='How to Fake an Uploaded File' data-link='https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/' data-app-id-name='category_above_content'></div>
<p>Our store software contains an extensive set of routines for processing uploaded images and resizing them into various image sizes. We&#8217;ve recently been adding some code to support a bulk import function and it&#8217;s become necessary to somehow fake uploading a file, given a specific URL for an image.</p>



<p>The basic plan is to use Net::HTTP to connect to the remote server and grab the image and save it in a temporary file. It turns out that Rails contains a UploadedTempfile class which is a subclass of Tempfile, and this is used by the CGI routines to handle any uploaded files.</p>



<span id="more-74"></span>



<p>So all we need to do is create our own instance of UploadedTempfile, set some attributes and then pass it along to our existing routines as if it were a real uploaded file.</p>



<pre class="wp-block-code"><code lang="ruby" class="language-ruby line-numbers">uri = URI.parse(image_url)
image_file_name = File.basename(uri.path) # Extract the file name
image_file = nil
begin
  timeout(10) do
    Net::HTTP.start(uri.host, uri.port) do |http|
      resp = http.get(uri.path)
      image_file = ActionController::UploadedTempfile.new(image_file_name)
      image_file.binmode
      image_file.write(resp.body)
      image_file.original_path = image_file_name
      image_file.rewind
    end
  end
rescue TimeoutError
  raise "Timeout connecting to #{uri.host}"
end
</code></pre>



<p>There are a few things that need to be set up, so that the fake upload file works properly.</p>



<p>The file is set to binary mode with</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>image_file.binmode</p></blockquote>



<p>The original file name is set with</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>image_file.original_path = image_file_name</p></blockquote>



<p>A content type attribute can also be set with</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>image_file.content_type</p></blockquote>



<p>but we don&#8217;t use this attribute, so it&#8217;s not set here.</p>



<p>Finally we rewind the file so that we can read it later on in the image processing routines. When we&#8217;re finished with the temporary file, we can delete it with a</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>image_file.close!</p></blockquote>
<div style='display:none;' class='shareaholic-canvas' data-app='share_buttons' data-title='How to Fake an Uploaded File' data-link='https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/' data-app-id-name='category_below_content'></div><div style='display:none;' class='shareaholic-canvas' data-app='recommendations' data-title='How to Fake an Uploaded File' data-link='https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/' data-app-id-name='category_below_content'></div>The post <a href="https://www.spiffystores.com.au/blog/2008/11/06/how-to-fake-an-uploaded-file/">How to Fake an Uploaded File</a> first appeared on <a href="https://www.spiffystores.com.au/blog">Spiffy Stores Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">74</post-id>	</item>
	</channel>
</rss>
