<feed xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
  <id>http://www.shiningthrough.co.uk/posts.xml</id>
  <updated>2010-03-11T00:25:17+00:00</updated>
  <title type="text">shiningthrough</title>
  <link href="http://www.shiningthrough.co.uk/posts.xml" rel="first" type="application/atom+xml"/>
  <link href="http://www.shiningthrough.co.uk/posts.xml" rel="last" type="application/atom+xml"/>
  <link href="http://www.shiningthrough.co.uk/posts.xml" rel="self" type="application/atom+xml"/>
  <rights>Copyright 2009</rights>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/1.xml</id>
    <title type="text">My first post!</title>
    <updated>2007-04-18T15:56:00Z</updated>
    <published>2007-04-18T15:56:00Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/1.xml"/>
    <content type="text">&lt;p&gt;My first blog post! and i am quite pleased with myself. I wrote this blog application from scratch using Ruby on Rails, its kind of an experiment, i get to try out new features, and generally use this site to mess around with.&lt;P&gt;
&lt;p&gt;In addition the XHTML and CSS that are used to make the layout are also a first for me. You'll notice that the grass always sticks to the bottom of the site, and the page grows from the middle instead of the bottom. This took some time to get working, and is still not 100%.&lt;p&gt;
&lt;p&gt;If you have any issues with the site not working 100% either graphically or functionally, i would really appreciate you letting me know.&lt;p&gt;</content>
    <link href="http://www.shiningthrough.co.uk/posts/1.xml" rel="edit"/>
  </entry>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/5.xml</id>
    <title type="text">Simple debugging in ruby on rails</title>
    <updated>2009-10-04T12:17:10Z</updated>
    <published>2007-04-24T11:37:00Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/5.xml"/>
    <content type="text">&lt;p&gt;I often find while writing a ruby on rails applications that i need to display the contents of a variable quickly and easily, this is straight forward in a view, but not so easy in a controller or model. There are a couple of ways to do this:&lt;p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;logger&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;debug &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;variable&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;will write to the development.log file, or if your using webrick as your development server, you can use:&lt;p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;support support_class support_class_ruby&quot;&gt;STDERR&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;puts &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;variable&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;and it will show up on the console output where your running your server.&lt;p&gt;</content>
    <link href="http://www.shiningthrough.co.uk/posts/5.xml" rel="edit"/>
  </entry>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/6.xml</id>
    <title type="text">Select helper methods in Ruby on Rails</title>
    <updated>2009-10-03T20:08:34Z</updated>
    <published>2007-05-01T14:41:00Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/6.xml"/>
    <content type="text">&lt;p&gt;It can get quite confusing when it comes to using the different select helpers, &lt;strong&gt;select&lt;/strong&gt;, &lt;strong&gt;select_tag&lt;/strong&gt; and &lt;strong&gt;collection_select&lt;/strong&gt; and in my opinion there is a lack of adequate documentation.&lt;br /&gt; For my own use if nothing more, i have done my best to compare the different form helper methods available, and when and how to use them.&lt;/p&gt;

&lt;p&gt;First we will look at the html for a basic selection box:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;text text_html text_html_basic&quot;&gt;&lt;span class=&quot;meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;entity entity_other entity_other_attribute-name entity_other_attribute-name_html&quot;&gt;name&lt;/span&gt;=&lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_html&quot;&gt;&quot;&lt;/span&gt;payment&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_html&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span class=&quot;meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;entity entity_other entity_other_attribute-name entity_other_attribute-name_html&quot;&gt;value&lt;/span&gt;=&lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_html&quot;&gt;&quot;&lt;/span&gt;1&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_html&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;VISA&lt;span class=&quot;meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span class=&quot;meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;entity entity_other entity_other_attribute-name entity_other_attribute-name_html&quot;&gt;value&lt;/span&gt;=&lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_html&quot;&gt;&quot;&lt;/span&gt;2&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_html&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;MasterCard&lt;span class=&quot;meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span class=&quot;meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html&quot;&gt;option&lt;/span&gt; &lt;span class=&quot;entity entity_other entity_other_attribute-name entity_other_attribute-name_html&quot;&gt;value&lt;/span&gt;=&lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_html&quot;&gt;&quot;&lt;/span&gt;3&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_html&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Switch&lt;span class=&quot;meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The selection box has some key parts, the name, which is required, and used by the browser when submitting the &amp;lt;select&amp;gt; choices to the server.&lt;/p&gt;

&lt;p&gt;The option tags, each of which are made up from a &quot;value&quot; and &quot;text&quot; pair, the &quot;value&quot; to identify the select item in the server, and the &quot;text&quot; which will be displayed to the user.&lt;/p&gt;

&lt;p&gt;There are three different select form helpers in ruby on rails, &quot;Select&quot;, &quot;select_tag&quot; and &quot;collection_select&quot;. We will have a closer look at each of them now.&lt;/p&gt;

&lt;h3&gt;select&lt;/h3&gt;
&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;select&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;object&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; method&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; choices&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; options &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; html_options &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;variable variable_other variable_other_constant variable_other_constant_ruby&quot;&gt;Defined&lt;/span&gt; &lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;support support_class support_class_ruby&quot;&gt;ActionView&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;support support_class support_class_ruby&quot;&gt;Helpers&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;variable variable_other variable_other_constant variable_other_constant_ruby&quot;&gt;FormOptionsHelper&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Use select when you require a basic drop-down selection box populated with data not sourced from a database.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The object is the name of an instance variable. This is typically a model object (singular name of the table whose data your displaying, or in other words, the table record).&lt;/li&gt;
&lt;li&gt;The method is the attribute of that instance variable. This is typically a field/column of the table whose data your displaying (really an ActiveRecord method).&lt;/li&gt;
&lt;li&gt;Together the object and method specify the name of the select statement in the generated html&lt;/li&gt;
&lt;li&gt;choices can be any enumerable object e.g arrays and hashes and results of database queries, and contains the option tags for the select box.&lt;/li&gt;
&lt;li&gt;The optional options argument takes various &quot;options&quot; some of which are listed below in the examples.&lt;/li&gt;
&lt;li&gt;The optional html_options argument allows css to be used for styling the select box.&lt;/li&gt;
&lt;li&gt;If one of the option tags in choices matches @object.method, that option tag will be selected&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this example a hash is being used to populate the option tags.&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; select&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;payment&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;id&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block&quot;&gt; &lt;/span&gt;&lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;Visa&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;1&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;Mastercard&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;2&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Select can be used in conjunction with a model object as seen in this example, an instance variable is passed into choices, but is being converted into an array of arrays.&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; select &lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;selected_payment&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;id&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby&quot;&gt;@&lt;/span&gt;payments&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;map &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;variable variable_other variable_other_block variable_other_block_ruby&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;[&lt;/span&gt;u&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;name&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt;u&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;id&lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The controller may look something like this:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby&quot;&gt;@&lt;/span&gt;payments&lt;/span&gt; &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;support support_class support_class_ruby&quot;&gt;Payment&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;find&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;all&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby&quot;&gt;@&lt;/span&gt;selected_payment&lt;/span&gt; &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby&quot;&gt;@&lt;/span&gt;payments&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;constant constant_numeric constant_numeric_ruby&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This will cause the third option_tag to be selected by default in the select box, but collection_select is preferable in this scenario.&lt;/p&gt;

&lt;h3&gt;select_tag&lt;/h3&gt;
&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;select_tag&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; option_tags &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;constant constant_language constant_language_ruby&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; options &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;variable variable_other variable_other_constant variable_other_constant_ruby&quot;&gt;Defined&lt;/span&gt; &lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;support support_class support_class_ruby&quot;&gt;ActionView&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;support support_class support_class_ruby&quot;&gt;Helpers&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;variable variable_other variable_other_constant variable_other_constant_ruby&quot;&gt;FormTagHelper&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Use select_tag when you require a drop-down selection box populated with data not sourced from a database, and are happy to hard code the default selected option tag. Select_tag should also be used when you want to process your form as a GET, rather than a POST, see &lt;a href=&quot;http://shiningthrough.co.uk/blog/show/7&quot;&gt;here&lt;/a&gt; for more details. Also note that select_tag does not have a html_options parameter&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The name specifies the name of the select statement in the generated html, and is used by the controller to access the selected items. &lt;/li&gt;
&lt;li&gt;The optional option_tags argument is a string containing the option tags for the select box.&lt;/li&gt;
&lt;li&gt;The optional options argument takes various &quot;options&quot; some of which are listed below in the examples.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Simple use of the select_tag helper:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; select_tag &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;payment&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;&amp;lt;option&amp;gt;VISA&amp;lt;/option&amp;gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;In this example, we are using &quot;options_for_select&quot; which accepts any container and returns a string of option tags, but we have defined an array. Note how the optional second parameter specifies the selected item in the list.&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; select_tag &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;payment&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; options_for_select&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;VISA&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;MasterCard&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;Switch&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;MasterCard&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Another option is this&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; select_tag &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;payment&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; options_for_select&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_lower string_quoted_other_literal_lower_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%w{&lt;/span&gt; VISA Mastercard Switch &lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;where %w is the shortcut notation for an array of strings that are space separated.&lt;/p&gt;

&lt;p&gt;You can also do multi-select boxes:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt; select_tag &lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;payment[]&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; options_for_select&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby&quot;&gt;@&lt;/span&gt;payments&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;multiple&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;constant constant_language constant_language_ruby&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;size&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;constant constant_numeric constant_numeric_ruby&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Note that the &quot;name&quot; in this case is an array, and @payments is a hash defined in the controller:&lt;/p&gt;

&lt;pre class=&quot;textmate-source&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby&quot;&gt;@&lt;/span&gt;payments&lt;/span&gt; &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;Visa&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;constant constant_numeric constant_numeric_ruby&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;Mastercard&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;constant constant_numeric constant_numeric_ruby&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;Switch&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;constant constant_numeric constant_numeric_ruby&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
	
&lt;p&gt;We can access the selected payment methods in the controller, using the array:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;params&lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;payment&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;select_tag does not support the :prompt option, which may not seem like an issue as we have already shown above how you can select a default option tag. But what if you want your form to GET, not POST, your only option is then to use select_tag, even if your data source is a database.&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; select_tag&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;payment&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;&amp;lt;option&amp;gt;-Select a payment method&amp;lt;/option&amp;gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby&quot;&gt;+&lt;/span&gt; options_from_collection_for_select&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby&quot;&gt;@&lt;/span&gt;payments&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;name&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;name&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;As select_tag takes a string, we can concatenate an additional string onto the beginning of the parameter, this allows us to add our default option_tag. We use options_from_collection_for_select which takes a value_method, and text_method (see collection_select for their definition) and an optional selected_value, and a collection, which can be defined in your controller like this:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby&quot;&gt;@&lt;/span&gt;payments&lt;/span&gt; &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;support support_class support_class_ruby&quot;&gt;Payment&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;find&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;all&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;order&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;name&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;h3&gt;collection_select&lt;/h3&gt;
&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;collection_select&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;object&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; method&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; collection&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; value_method&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; text_method&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; options &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; html_options &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;variable variable_other variable_other_constant variable_other_constant_ruby&quot;&gt;Defined&lt;/span&gt; &lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;support support_class support_class_ruby&quot;&gt;ActionView&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;support support_class support_class_ruby&quot;&gt;Helpers&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_other punctuation_separator_other_ruby&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;variable variable_other variable_other_constant variable_other_constant_ruby&quot;&gt;FormOptionsHelper&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Use collection_select when you require a drop-down selection box, whose source is a model/object&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The object is the singular name of the table whose data your displaying (the table record).&lt;/li&gt;
&lt;li&gt;The method is the field/column of the the relevant data (really an ActiveRecord method).&lt;/li&gt;
&lt;li&gt;Together the object and method specify the name of the select statement in the generated html &lt;/li&gt;
&lt;li&gt;collection takes the option tags for the select box, this can be a hash or array.&lt;/li&gt;
&lt;li&gt;value_method is the field/column to use for the value of the option tags in your html.&lt;/li&gt;
&lt;li&gt;text_method is the field/column to use for the visible text of the option tags in your html.&lt;/li&gt;
&lt;li&gt;The optional options argument takes various &quot;options&quot; some of which are listed below in the examples.&lt;/li&gt;
&lt;li&gt;The optional html_options argument allows css to be used for styling the select box.&lt;/li&gt;
&lt;li&gt;If one of the option tags in collection matches @object.method, that option tag will be selected&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this example the payment methods are being added to the select box via a model object, notice how we use the option, :prompt to add an additional option tag to the select box, which will be selected by default. Note that if @object.method matches one of the option tags, this will be selected by default, and :prompt wont appear in the list.&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; collection_select&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;payment&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;id&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby&quot;&gt;@&lt;/span&gt;payments&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;id&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;name&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; options &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;prompt&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;-Select a payment&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;class&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;payment&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The selected item can be accessed in the controller, by using:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;selected_payment &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; params&lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;[&lt;/span&gt;payment&lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;][&lt;/span&gt;id&lt;span class=&quot;punctuation punctuation_section punctuation_section_array punctuation_section_array_ruby&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;A final Note to say that the parameters used in all the helpers may be strings or symbols, symbols may be a better choice in some situations, but thats another article.&lt;/p&gt;
</content>
    <link href="http://www.shiningthrough.co.uk/posts/6.xml" rel="edit"/>
  </entry>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/7.xml</id>
    <title type="text">GET and POST in Ruby on Rails</title>
    <updated>2009-10-05T12:55:59Z</updated>
    <published>2007-05-24T11:42:59Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/7.xml"/>
    <content type="text">&lt;p&gt;Lets start with the basics.....&lt;/p&gt;

&lt;h3&gt;What are GET and POST?&lt;/h3&gt;

&lt;p&gt;GET and POST specify the two main ways by which the browser sends a form's data to the server for processing.&lt;/p&gt;

&lt;p&gt;The Get method contacts the server, and sends the forms data in a single transmission step. The browser appends the data to the forms action URL, separated by the question mark character.&lt;/p&gt;

&lt;p&gt;With the POST method, the browser sends the data in two steps. The browser first contacts the server specified in the action attribute, when contact is made the data is sent to the server in a separate transparent transmission.&lt;/p&gt;

&lt;p&gt;How does this fit in with Ruby on Rails? a rails form will process its data by default as a POST unless you specify otherwise:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; form_tag&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;action&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;find_holidays_by_country&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;method&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;get&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;
&amp;lt;%= end_form_tag %&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Why would you want your form to be a GET? there are many reasons, imagine that a form is used for navigation. After the user has selected their required country, and performed a search, they may want to bookmark the resulting page, this is only possible by use of GET. The following code illustrates this:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; form_tag&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;action&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;list_holidays_by_country&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;method&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;get&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;
  &amp;lt;%= select_tag(:country, options_from_collection_for_select(@countries, :name, :name)) %&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; submit_tag &lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;Search&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;name&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;constant constant_language constant_language_ruby&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;
&amp;lt;%= end_form_tag %&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
Note that select_tag is used as the helper as it is compatible with a GET request, select, and collection_select are not.&lt;br /&gt;Also note that the name of the country is returned in the URL (more meaningful than an id), as well as being used as the text in the select box. See &lt;a href=&quot;http://shiningthrough.co.uk/blog/show/6&quot;&gt;here&lt;/a&gt; for more details.&lt;br /&gt;The submit tag is set to nil, so the button does not show up in the URL.
&lt;/p&gt;

&lt;p&gt;For a discussion on which method is most appropriate for a given problem, this is a very useful article &lt;a href =&quot;http://www.w3.org/2001/tag/doc/whenToUseGet.html&quot;&gt;When To Use GET&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lastly a warning when using GET requests. If an action changes the state of something on the server make sure the request is either a POST, or if GET must be used, make sure some kind of human intervention immediately follows.&lt;br /&gt; Search engine spiders, and &lt;a href=&quot;http://webaccelerator.google.com/&quot;&gt;web accelerators&lt;/a&gt; follow links, and can cause these actions to trigger unintentionally.&lt;/p&gt;</content>
    <link href="http://www.shiningthrough.co.uk/posts/7.xml" rel="edit"/>
  </entry>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/8.xml</id>
    <title type="text">Safari 3 bugs</title>
    <updated>2007-06-22T10:11:00Z</updated>
    <published>2007-06-22T10:11:00Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/8.xml"/>
    <content type="text">&lt;p&gt;Like over a million others (at time of writing) i have upgraded to Safari 3. There are some great new features which are covered in other blog posts, but i am also encountering a few problems...&lt;/p&gt;
&lt;p&gt;Apple's new browser crashes on me at least once today, but it is still only a beta, but one problem i found was its lack of support for the pseudo-class, &lt;strong&gt;:last-child&lt;/strong&gt;.&lt;p&gt;
&lt;p&gt;&lt;span class=&quot;safari3bug&quot;&gt;First Child &lt;/span&gt;&lt;span class=&quot;safari3bug&quot;&gt;Second Child&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The above example when viewed in firefox displays the text &quot;Second Child&quot; in Red, but not in Safari 3 beta.&lt;br /&gt;
	Perhaps its this bug that's causing apple.com's cinema screen page to render incorrectly:&lt;/p&gt;
&lt;img alt=&quot;Apple.com broken in Safari 3 Beta&quot; src=&quot;/images/safari3_bug.png&quot; /&gt;
&lt;p&gt;Has anyone else noticed any css related deficiencies?&lt;/p&gt;</content>
    <link href="http://www.shiningthrough.co.uk/posts/8.xml" rel="edit"/>
  </entry>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/9.xml</id>
    <title type="text">Reindex spotlight in OS X Leopard</title>
    <updated>2007-12-15T11:19:00Z</updated>
    <published>2007-12-15T11:19:00Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/9.xml"/>
    <content type="text">&lt;p&gt;I recently upgraded to OS x Leopard, and afterwards noticed that spotlight was failing to find certain applications. It was necessary to delete the index database in order to force reindexing. This is done using the mdutil application.&lt;/p&gt;
&lt;p&gt;You can find out what it does by opening a terminal window, and typing:&lt;/p&gt;
&lt;pre&gt;man mdutil&lt;/pre&gt;
&lt;p&gt;But, if you just want to straight ahead and remove the index database, open a terminal window, and type the following:&lt;/p&gt;
&lt;pre&gt;sudo mdutil -E /&lt;/pre&gt;</content>
    <link href="http://www.shiningthrough.co.uk/posts/9.xml" rel="edit"/>
  </entry>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/10.xml</id>
    <title type="text">Use Photoshop to remove Mac OS X Leopard window shadows from screenshots</title>
    <updated>2008-12-02T21:42:00Z</updated>
    <published>2008-12-02T21:42:00Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/10.xml"/>
    <content type="text">&lt;img alt=&quot;OS X Leopard Screenshot without shadows&quot; src=&quot;/images/no_shadow.png&quot;/&gt;
&lt;p&gt;Whilst i was writing a recent article it was necessary for me to take some dialog screenshots from OS X Leopard. There are several ways of doing this:&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;Using the grab utility that comes with OS X.&lt;/li&gt;
	&lt;li&gt;Holding Apple, ctrl, shift and 4, release, then press space.&lt;/li&gt;
	&lt;li&gt;Entering &quot;screencapture -ic&quot; on the command line, then press space. (This appears to be the same capture as above)&lt;/li&gt;
	&lt;li&gt;Use one of the many third party applications available, including &lt;a href=&quot;http://projects.digitalwaters.net/index.php?q=instantshot&quot;&gt;InstantShot&lt;/a&gt;, &lt;a href=&quot;http://www.ambrosiasw.com/utilities/snapzprox/&quot;&gt;SnapzProX&lt;/a&gt;, &lt;a href=&quot;http://www.jingproject.com/&quot;&gt;Jing&lt;/a&gt; and &lt;a href=&quot;http://www.yellowmug.com/snapndrag/&quot;&gt;SnapNDrag&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The problem i have with all these is that either they capture the window along with the large drop shadow that Leopard uses, or they capture the window without the drop shadow, but the rounded corners look poor and the edge border is missing&lt;/p&gt;

&lt;p&gt;I have come up with a few Photoshop steps that reconstruct the dialog corners and edges, and also allow you, should you wish, to add a smaller drop shadow back in.&lt;/p&gt;

&lt;ul&gt;
	&lt;li&gt;Take a screen shot, using Apple, ctrl, shift and 4, then pressing space to capture a window.&lt;/li&gt;
	&lt;li&gt;Create a new Photoshop document based on the clipboard, and set the background colour to be white.&lt;/li&gt;
	&lt;li&gt;Paste the screenshot in, it should look something like the dialog above, but with a black background.&lt;/li&gt;
	&lt;li&gt;Using the magic wand tool, with anti-alias switched off, and a tolerance set to 100, select the black region of the image where the shadow would be.&lt;/li&gt;
	&lt;li&gt;Click the &quot;Add layer mask&quot; button to create a mask based on that selection.&lt;/li&gt;
	&lt;li&gt;Adjust the mask as necessary, to make sure only the window is masked.&lt;/li&gt;
	&lt;li&gt;Inverse the mask by pressing Apple and i, to make the shadow masked.&lt;/li&gt;
	&lt;li&gt;Add a layer style to the masked window, select stroke with the following settings: and set the gradient from #7f7f7f to #000000&lt;img alt=&quot;OS X Leopard Screenshot without shadows&quot; src=&quot;/images/stroke_settings.png&quot;/&gt;&lt;/li&gt;
	&lt;li&gt;Select Inner Shadow with the following settings:&lt;br /&gt;&lt;img alt=&quot;Inner Shadow Settings&quot; src=&quot;/images/inner_shadow_settings.png&quot;/&gt;&lt;/li&gt;
	&lt;li&gt;And if so desired, select Drop Shadow with the following settings:&lt;br /&gt;&lt;img alt=&quot;Drop Shadow Settings&quot; src=&quot;/images/drop_shadow_settings.png&quot;/&gt;&lt;/li&gt;
	&lt;li&gt;I have placed these styles in a downloadable layer style &lt;a href=&quot;Leopard Window Edge.asl.zip&quot;&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;</content>
    <link href="http://www.shiningthrough.co.uk/posts/10.xml" rel="edit"/>
  </entry>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/11.xml</id>
    <title type="text">Install ImageScience on OS X Leopard</title>
    <updated>2009-02-15T10:01:00Z</updated>
    <published>2009-02-15T10:01:00Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/11.xml"/>
    <content type="text">&lt;p&gt;I couldn&amp;rsquo;t find a tutorial that took me through this process successfully, in the end I combined information from several other articles to provide these simple steps.&lt;/p&gt;

&lt;h2&gt;Prerequisites&lt;/h2&gt;
&lt;p&gt;I used a clean fresh Install of Leopard to begin with.&lt;/p&gt;
&lt;p&gt;Make sure you have installed Xcode from the Leopard disk.&lt;/p&gt;

&lt;h2&gt;Install ruby_inline&lt;/h2&gt;
&lt;p&gt;Open a terminal, and enter:&lt;/p&gt;

&lt;pre&gt;
sudo gem install RubyInline
&lt;/pre&gt;

&lt;h2&gt;Install FreeImage&lt;/h2&gt;
&lt;p&gt;Download FreeImage (http://freeimage.sourceforge.net/download.html)&lt;/p&gt;
&lt;p&gt;Using your editor of choice (I am using Vi), edit Makefile.osx&lt;/p&gt;
&lt;pre&gt;vi ~/Downloads/FreeImage/Makefile.osx&lt;/pre&gt;

&lt;p&gt;to make sure that the following lines read as: (correct for FreeImage 3.11.0)&lt;/p&gt;

&lt;pre&gt;
INCLUDE_PPC = -isysroot /Developer/SDKs/MacOSX10.5.sdk
INCLUDE_I386 = -isysroot /Developer/SDKs/MacOSX10.5.sdk
...
LIBRARIES_PPC = -Wl,-syslibroot /Developer/SDKs/MacOSX10.5.sdk
LIBRARIES_I386 = -Wl,-syslibroot /Developer/SDKs/MacOSX10.5.sdk
&lt;/pre&gt;

&lt;p&gt;then, in a terminal:&lt;/p&gt;

&lt;pre&gt;
cd FreeImage
make
sudo make install
&lt;/pre&gt;

&lt;h2&gt;Install ImageScience&lt;/h2&gt;
&lt;pre&gt;sudo gem install image_science&lt;/pre&gt;

&lt;h2&gt;Test it all worked&lt;/h2&gt;

&lt;pre&gt;
irb
require 'image_science'
&lt;/pre&gt;
&lt;p&gt;It will return true if you have been successful.&lt;/p&gt;

&lt;p&gt;With thanks too&lt;/p&gt;
&lt;a href=&quot;http://www.ruby-forum.com/topic/141754&quot; title=&quot;www.Ruby-Forum.com&quot;&gt;http://www.ruby-forum.com/topic/141754&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://resettodefault.com/2007/11/05/imagescience_on_leopard&quot; title=&quot;www.resettodefault.com&quot;&gt;http://resettodefault.com/2007/11/05/imagescience_on_leopard&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://seattlerb.rubyforge.org/ImageScience.html&quot; title=&quot;ImageScience Site&quot;&gt;http://seattlerb.rubyforge.org/ImageScience.html&lt;/a&gt;&lt;br /&gt;</content>
    <link href="http://www.shiningthrough.co.uk/posts/11.xml" rel="edit"/>
  </entry>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/12.xml</id>
    <title type="text">Install RMagick and ImageMagick on Leopard OS X, quickly and simply</title>
    <updated>2009-10-04T19:16:22Z</updated>
    <published>2009-02-15T10:01:00Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/12.xml"/>
    <content type="text">&lt;p&gt;&lt;a href=&quot;http://rmagick.rubyforge.org/&quot; title=&quot;RMagick&quot;&gt;RMagick&lt;/a&gt; is a Ruby interface that allows access to the &lt;a href=&quot;http://www.imagemagick.org/script/index.php&quot; title=&quot;ImageMagick&quot;&gt;ImageMagick&lt;/a&gt; image processing library. When I started out developing in Ruby on Rails, installing RMagick was quite an endeavour, which was one of the reasons I used ImageSciene in preference. The installation has now become very simple indeed.&lt;/p&gt;

&lt;p&gt;Go to the &lt;a href=&quot;http://rubyforge.org/frs/?group_id=12&amp;release_id=16101&quot; title=&quot;RMagick&quot;&gt;RMagick&lt;/a&gt; download page, and download the rmagick-osx-installer (at the time of writing, this was rm_install-1.2.1.zip)&lt;/p&gt;
&lt;p&gt;Open a terminal window, and enter the following command (Leopard will download RMagick to the Downloads directory, and automatically unzip it there)&lt;/p&gt;
&lt;pre&gt;sudo ruby ~/Downloads/rm_install-1/rm_install.rb&lt;/pre&gt;
&lt;p&gt;And thats it! I was very impressed with its simplicity this time round.&lt;/p&gt;</content>
    <link href="http://www.shiningthrough.co.uk/posts/12.xml" rel="edit"/>
  </entry>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/13.xml</id>
    <title type="text">AuthLogic</title>
    <updated>2009-10-05T11:24:56Z</updated>
    <published>2009-07-14T16:08:00Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/13.xml"/>
    <content type="text">&lt;p&gt;I know that it is rather old news by now, but I just discovered the wonders of Authlogic.  If you have any sort of private interface to your app, whether it is adding products to your store or just adding comments to a forum, you may want some sort of authentication to keep the Viagra-toting spam-hordes at bay. &lt;/p&gt;

&lt;p&gt;The great thing about Authlogic is that it is very light and adaptable, especially as opposed to that bastion of yore, Restful Authentication.  You can use Authlogic by itself, and have a perfectly secure application, or you can use it with any number of plugins, from openid to facebook, so that the wonders of having just one password/login combination can be yours.  &lt;/p&gt;

&lt;p&gt;As usual, Ryan Bates has a wonderful railscast on the matter, that gives a very thorough introduction to Authlogic. Check it out: &lt;a href=&quot;http://railscasts.com/episodes/160-authlogic&quot;&gt;http://railscasts.com/episodes/160-authlogic&lt;/a&gt; &lt;/p&gt;</content>
    <link href="http://www.shiningthrough.co.uk/posts/13.xml" rel="edit"/>
  </entry>
  <entry xml:lang="en" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.shiningthrough.co.uk/posts/17.xml</id>
    <title type="text">Dynamic multiple image uploads with Ruby on Rails</title>
    <updated>2010-02-25T19:09:45Z</updated>
    <published>2010-02-23T15:21:43Z</published>
    <app:control>
      <app:draft>no</app:draft>
    </app:control>
    <author>
      <name>someone</name>
    </author>
    <link href="http://www.shiningthrough.co.uk/posts/17.xml"/>
    <content type="text">&lt;p&gt;This tutorial explains how to create a simple web application that allows a user to upload one, or multiple images. It makes use of the paperclip plugin, Ajax using RJS and new multi-model form functionality that has arrived with Rails 2.3.3 in the form of accepts_nested_attributes_for.&lt;/p&gt;

&lt;p&gt;Make sure your up to date with Rails, you will need at least 2.3.3&lt;/p&gt;

&lt;p&gt;Lets start by making a new rails application&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;text text_plain&quot;&gt;&lt;span class=&quot;meta meta_paragraph meta_paragraph_text&quot;&gt;rails shop&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Lets add some scaffolding to our new application ...&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_shell&quot;&gt;script/generate scaffold product name:string price:decimal&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Next we create a model class that will hold our photos:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_shell&quot;&gt;script/generate model photo&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now we need to install the paperclip plugin, you need to have git installed for this:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_shell&quot;&gt;script/plugin install git://github.com/thoughtbot/paperclip.git&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Run the Paperclip generator, this will create the appropriate migrations.&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_shell&quot;&gt;script/generate paperclip photo data&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Then we just run the migrations:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_shell&quot;&gt;rake db:migrate&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Right, thats everything setup, now its time to delve into the code. First thing to do is setup the models. The photo model should look like this:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;meta meta_rails meta_rails_model&quot;&gt;&lt;span class=&quot;meta meta_class meta_class_ruby&quot;&gt;&lt;span class=&quot;keyword keyword_control keyword_control_class keyword_control_class_ruby&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby&quot;&gt;Photo&lt;span class=&quot;entity entity_other entity_other_inherited-class entity_other_inherited-class_ruby&quot;&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_inheritance punctuation_separator_inheritance_ruby&quot;&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Base&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;support support_function support_function_activerecord support_function_activerecord_rails&quot;&gt;belongs_to&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;product&lt;/span&gt;
  has_attached_file &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;data&lt;/span&gt;  
&lt;/span&gt;&lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The first line sets up half the relationship between Photo and Product. The second line adds the paperclip attachment to our Photo model, there are lot of optional settings that can be made here, but are beyond the scope of this tutorial, see the tutorial in the references for more information on paperclip. For now lets keep things simple.&lt;/p&gt;

&lt;p&gt;The product model should look like this:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;meta meta_rails meta_rails_model&quot;&gt;&lt;span class=&quot;meta meta_class meta_class_ruby&quot;&gt;&lt;span class=&quot;keyword keyword_control keyword_control_class keyword_control_class_ruby&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby&quot;&gt;Product&lt;span class=&quot;entity entity_other entity_other_inherited-class entity_other_inherited-class_ruby&quot;&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_inheritance punctuation_separator_inheritance_ruby&quot;&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Base&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;support support_function support_function_activerecord support_function_activerecord_rails&quot;&gt;has_many&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;photos&lt;/span&gt;
  &lt;span class=&quot;support support_function support_function_activerecord support_function_activerecord_rails&quot;&gt;accepts_nested_attributes_for&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;photos&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;allow_destroy&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;constant constant_language constant_language_ruby&quot;&gt;true&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The first line specifies that we want one or more photos to be associated with our product.
The second line is where the magic happens, this is new in rails 2.3.3, this specifies that photos is a nested attribute, this allows you to save attributes on associated 'Photo' records through the parent, 'Product'.&lt;/p&gt;

&lt;h3&gt;The View Code&lt;/h3&gt;

&lt;p&gt;That's the hard part done, all we have to do now is write the view code. In most scenarios, the number of images you want to add will be variable, therefore we will allow the user to dynamically add as many images as required. This kind of functionality means javascript, so lets add in the prototype libraries in products.html.erb.&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;head&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;....&lt;/span&gt;
  &lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; javascript_include_tag &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;defaults&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;
&amp;lt;/head&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Firstly lets add the view code necessary to create a new photo, add the following to your new.html.erb file, within the form_for:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; add_photo&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;And also add file upload support to the form, by adding :multipart =&gt; true to the form_for, as shown here:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;support support_function support_function_viewhelpers support_function_viewhelpers_rails&quot;&gt;form_for&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby&quot;&gt;@&lt;/span&gt;product&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;html&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;multipart&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;constant constant_language constant_language_ruby&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby&quot;&gt;do &lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;variable variable_other variable_other_block variable_other_block_ruby&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Next we will add the implementation for the add_photo function. Go to products_helper.rb, and add the following funtion:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby&quot;&gt;&lt;span class=&quot;keyword keyword_control keyword_control_def keyword_control_def_ruby&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;entity entity_name entity_name_function entity_name_function_ruby&quot;&gt;add_photo&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;variable variable_parameter variable_parameter_function variable_parameter_function_ruby&quot;&gt;form_builder&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
  link_to_function &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;add&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;id&lt;/span&gt;  &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;add_photo&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby&quot;&gt;do &lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;variable variable_other variable_other_block variable_other_block_ruby&quot;&gt;page&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby&quot;&gt;|&lt;/span&gt;
    form_builder&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;support support_function support_function_viewhelpers support_function_viewhelpers_rails&quot;&gt;fields_for&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;photos&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;support support_class support_class_ruby&quot;&gt;Photo&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;child_index&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;NEW_RECORD&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby&quot;&gt;do &lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;variable variable_other variable_other_block variable_other_block_ruby&quot;&gt;photo_form&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_variable punctuation_separator_variable_ruby&quot;&gt;|&lt;/span&gt;
      html &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;support support_function support_function_actionpack support_function_actionpack_rails&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;partial&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_single string_quoted_single_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;'&lt;/span&gt;photo&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;locals&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block&quot;&gt; &lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;f&lt;/span&gt; &lt;span class=&quot;punctuation punctuation_separator punctuation_separator_key-value&quot;&gt;=&amp;gt;&lt;/span&gt; photo_form &lt;span class=&quot;punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt;
      page &lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;$('add_photo').insert({ before: '&lt;span class=&quot;source source_ruby source_ruby_embedded source_ruby_embedded_source&quot;&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby&quot;&gt;#{&lt;/span&gt;escape_javascript&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;html&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby&quot;&gt;}&lt;/span&gt;&lt;/span&gt;'.replace(/NEW_RECORD/g, new Date().getTime()) });&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This is a little bit complex, so lets go through it.&lt;/p&gt;

&lt;p&gt;link_to_funtion is a helper found in the ActionView library, you provide it with some link text, and some JavaScript to execute when the link is clicked, it is important for this example that the link is given an id, you will see why shortly. We must build up a string to be executed within our link_to_funtion block, so we create a model scope for our new photo using form_for. Within this new scope we evaluate a partial(we have not written this yet) and store it locally, we then pass this evaluated partial into some prototype javascript that will insert the rendered HTML into the page. The point at which it is inserted is specified above as inserting 'before' the element with id add_photo, that is why the link was given an id, so that the HTML could be inserted before it.&lt;/p&gt;

&lt;p&gt;The last thing that needs adding is the partial that was mentioned above. Create a new file in your app/views/products directory called _photo.html.erb (the underscore prefix is a Rails convention that specifies it as a partial), and paste in the following code:&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;fieldset&lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;keyword keyword_operator keyword_operator_logical keyword_operator_logical_ruby&quot;&gt;!&lt;/span&gt;f&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;object&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;new_record? &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;
        &amp;lt;%= image_tag f.object.data.url %&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;
    
    &amp;lt;%= f.file_field :data %&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class=&quot;keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby&quot;&gt;%=&lt;/span&gt; delete_photo&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;f&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_other string_quoted_other_literal string_quoted_other_literal_other string_quoted_other_literal_other_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;%&amp;gt;&lt;/span&gt;
&amp;lt;/fieldset&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;We use a fieldset tag so we can group related items together, in this case its an image, a file upload input tag, and a delete link that runs some javascript. The fieldset allows that delete link to target the element that encompasses it. Next, we check if the object we are displaying is a new record, if its not, then we have an existing photo, so lets display it with an image_tag. The final two lines here provide a file upload input tag, and a call to a helper method that will create the delete link, lets have a look at that helper now.&lt;/p&gt;

&lt;pre class=&quot;textmate-source twilight&quot;&gt;&lt;span class=&quot;source source_ruby source_ruby_rails&quot;&gt;&lt;span class=&quot;meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby&quot;&gt;&lt;span class=&quot;keyword keyword_control keyword_control_def keyword_control_def_ruby&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;entity entity_name entity_name_function entity_name_function_ruby&quot;&gt;delete_photo&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;variable variable_parameter variable_parameter_function variable_parameter_function_ruby&quot;&gt;form_builder&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
  &lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;if&lt;/span&gt; form_builder&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;object&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;new_record?
    link_to_function&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;Remove this Photo&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;this.up('fieldset').remove()&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;else&lt;/span&gt;
    form_builder&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;support support_function support_function_viewhelpers support_function_viewhelpers_rails&quot;&gt;hidden_field&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;constant constant_other constant_other_symbol constant_other_symbol_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby&quot;&gt;:&lt;/span&gt;_delete&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby&quot;&gt;+&lt;/span&gt;
    link_to_function&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;Remove this Photo&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;string string_quoted string_quoted_double string_quoted_double_ruby&quot;&gt;&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby&quot;&gt;&quot;&lt;/span&gt;this.up('fieldset').hide(); $(this).previous().value = '1'&lt;span class=&quot;punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword keyword_control keyword_control_ruby&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;The first thing we do in delete_photo is to check if we are dealing with a new record or not, if it is, then we can simply delete the fieldset element we marked up previously. If, however, we have an actual photo, then it needs to be deleted from the database. Because we set :allow_destroy =&gt; true for accepts_nested_attributes_for, we can use the virtual attribute _delete in order to delete the child record.&lt;/p&gt;

&lt;p&gt;And that wraps it up, try it out, you are now able to add and remove multiple images dynamically!&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;
&lt;p&gt;http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/&lt;br /&gt;
http://railsforum.com/viewtopic.php?id=28447&lt;br /&gt;
http://infrastacks.com/?p=57&lt;/p&gt;</content>
    <link href="http://www.shiningthrough.co.uk/posts/17.xml" rel="edit"/>
  </entry>
</feed>
