<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Need help building complex form in Rails</title>
	<atom:link href="http://www.fuzzylizard.com/archives/2007/06/08/887/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fuzzylizard.com/archives/2007/06/08/887/</link>
	<description>My thoughts on Agile, Java and Ruby on Rails (mostly)</description>
	<pubDate>Thu, 04 Dec 2008 01:23:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
		<item>
		<title>By: Chris Johnston</title>
		<link>http://www.fuzzylizard.com/archives/2007/06/08/887/#comment-13131</link>
		<dc:creator>Chris Johnston</dc:creator>
		<pubDate>Sat, 09 Jun 2007 14:38:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.fuzzylizard.com/archives/2007/06/08/887/#comment-13131</guid>
		<description>Thanks for trying. I have been having some weird things happen when people try and post comments with Textile getting in the way as well as the restrictions placed on comments to help battle spam. Maybe it is time I turned off Textile, hmmm?

I will muddle through what you have posted Clint and probably post follow up questions. Thanks for the suggestions.</description>
		<content:encoded><![CDATA[<p>Thanks for trying. I have been having some weird things happen when people try and post comments with Textile getting in the way as well as the restrictions placed on comments to help battle spam. Maybe it is time I turned off Textile, hmmm?</p>
<p>I will muddle through what you have posted Clint and probably post follow up questions. Thanks for the suggestions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Clint Bishop</title>
		<link>http://www.fuzzylizard.com/archives/2007/06/08/887/#comment-13129</link>
		<dc:creator>Clint Bishop</dc:creator>
		<pubDate>Sat, 09 Jun 2007 07:34:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.fuzzylizard.com/archives/2007/06/08/887/#comment-13129</guid>
		<description>Ok so pre and code tags don't work at all and this is becoming a fiasco.  Suffice it to say that I'm not going to try to copy code again, so for the amount field, I'd have this in ERb tags:

form.text_field :"amount_#{index}"

and for the expense_report field, this:

form.select("expense_category_#{index}",@expense_categories)

where @expense_categories is found in the new action with:

@expense_categories = ExpenseCategory.find(:all).map {&#124;cat&#124; [cat.category, cat.id]}


Man, not my night for posting blog comments, apparently.</description>
		<content:encoded><![CDATA[<p>Ok so pre and code tags don&#8217;t work at all and this is becoming a fiasco.  Suffice it to say that I&#8217;m not going to try to copy code again, so for the amount field, I&#8217;d have this in ERb tags:</p>
<p>form.text_field :&#8221;amount_#{index}&#8221;</p>
<p>and for the expense_report field, this:</p>
<p>form.select(&#8221;expense_category_#{index}&#8221;,@expense_categories)</p>
<p>where @expense_categories is found in the new action with:</p>
<p>@expense_categories = ExpenseCategory.find(:all).map {|cat| [cat.category, cat.id]}</p>
<p>Man, not my night for posting blog comments, apparently.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Clint Bishop</title>
		<link>http://www.fuzzylizard.com/archives/2007/06/08/887/#comment-13128</link>
		<dc:creator>Clint Bishop</dc:creator>
		<pubDate>Sat, 09 Jun 2007 07:29:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.fuzzylizard.com/archives/2007/06/08/887/#comment-13128</guid>
		<description>Ok that code totally didn't post like I thought it would.  Hate to say I told you so on the Textile.

The template:

&lt;code&gt;
 { :action =&#62; 'create'} do &#124;form&#124; %&#62;

  Name
   40  %&#62;

  Report Submitted on
  


  

    Report Line 
    
     
    
  

  Comments
   5, :cols =&#62; 60 %&#62;
  


&lt;/code&gt;


And the controller:

&lt;code&gt;
class ExpenseReportsController "Create", "report"=&#62;{"expense_category_0"=&#62;"1", "submit_date(3i)"=&#62;"9", 
    # "submit_date(4i)"=&#62;"02", "expense_category_1"=&#62;"1", "submit_date(5i)"=&#62;"41", 
    # "comments"=&#62;"This was an awesome trip.", "amount_0"=&#62;"100", "expense_category_2"=&#62;"4", 
    # "project"=&#62;"Everybody party", "amount_1"=&#62;"200", "expense_category_3"=&#62;"2", "amount_2"=&#62;"300", 
    #     "expense_category_4"=&#62;"3", "amount_3"=&#62;"400", "amount_4"=&#62;"500", "submit_date(1i)"=&#62;"2007", "submit_date(2i)"=&#62;"6"}}
    #
    # nab the params you need based on the index (expense_category_x with amount_x) and save each.
  end
end
&lt;/code&gt;
</description>
		<content:encoded><![CDATA[<p>Ok that code totally didn&#8217;t post like I thought it would.  Hate to say I told you so on the Textile.</p>
<p>The template:</p>
<p><code><br />
 { :action =&gt; 'create'} do |form| %&gt;</p>
<p>  Name<br />
   40  %&gt;</p>
<p>  Report Submitted on</p>
<p>    Report Line </p>
<p>  Comments<br />
   5, :cols =&gt; 60 %&gt;</p>
<p></code></p>
<p>And the controller:</p>
<p><code><br />
class ExpenseReportsController "Create", "report"=&gt;{"expense_category_0"=&gt;"1", "submit_date(3i)"=&gt;"9",<br />
    # "submit_date(4i)"=&gt;"02", "expense_category_1"=&gt;"1", "submit_date(5i)"=&gt;"41",<br />
    # "comments"=&gt;"This was an awesome trip.", "amount_0"=&gt;"100", "expense_category_2"=&gt;"4",<br />
    # "project"=&gt;"Everybody party", "amount_1"=&gt;"200", "expense_category_3"=&gt;"2", "amount_2"=&gt;"300",<br />
    #     "expense_category_4"=&gt;"3", "amount_3"=&gt;"400", "amount_4"=&gt;"500", "submit_date(1i)"=&gt;"2007", "submit_date(2i)"=&gt;"6"}}<br />
    #<br />
    # nab the params you need based on the index (expense_category_x with amount_x) and save each.<br />
  end<br />
end<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Clint Bishop</title>
		<link>http://www.fuzzylizard.com/archives/2007/06/08/887/#comment-13127</link>
		<dc:creator>Clint Bishop</dc:creator>
		<pubDate>Sat, 09 Jun 2007 07:24:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.fuzzylizard.com/archives/2007/06/08/887/#comment-13127</guid>
		<description>Hey, the quick and easy solution is this:

new.rhtml:

&lt;code&gt;
 { :action =&#62; 'create'} do &#124;form&#124; %&#62;

  Name
   40  %&#62;

  Report Submitted on
  


  

    Report Line 
    
     
    
  

  Comments
   5, :cols =&#62; 60 %&#62;
  


&lt;/code&gt;

expense_reports_controller.rb:

&lt;code&gt;
class ExpenseReportsController "Create", "report"=&#62;{"expense_category_0"=&#62;"1", "submit_date(3i)"=&#62;"9", 
    # "submit_date(4i)"=&#62;"02", "expense_category_1"=&#62;"1", "submit_date(5i)"=&#62;"41", 
    # "comments"=&#62;"This was an awesome trip.", "amount_0"=&#62;"100", "expense_category_2"=&#62;"4", 
    # "project"=&#62;"Everybody party", "amount_1"=&#62;"200", "expense_category_3"=&#62;"2", "amount_2"=&#62;"300", 
    #     "expense_category_4"=&#62;"3", "amount_3"=&#62;"400", "amount_4"=&#62;"500", "submit_date(1i)"=&#62;"2007", "submit_date(2i)"=&#62;"6"}}
    #
    # nab the params you need based on the index (expense_category_x with amount_x) and save each.
  end
end
&lt;/code&gt;

(pardon the possibly messy comment, I'm a little special when it comes to textile)


In short, I eschewed the collection_select thing in favor of just a plain ol' select that gets mapped in the controller action.  You're going to need that to be populated with every expense_category you've got. I got rid of the fields_for stuff because the expense_categorIES instance variable doesn't actually hang off of line_item.  Although you have to assign a single expense_categorY to the line_item, you'll just need a select * of the categories table.

As to your two problems stated above, 'line_items' won't actually appear as a key in the params hash;  line_item_form will.  The best way to inspect your params is to just do

def create
  raise params.inspect
end

Or to use the TextMate Footnotes plugin, which displays all your state at the bottom of the page when in dev mode.

As to your second problem, you're giving the text_field for amount the same name every time (:amount), so the form parameters for the five fields are being overwritten in series, since you'll only be getting one :amount key in the params.  You'll need to stick that id on the end to get unique names.

If it could be possible, it would be nice to somehow massage fields_for to break your parameters into sub-hashes to keep from having to do as much parsing of params, but I'm not sure how to do it without a little more prodding.  Anyway, HTH, fellow TWer.

Clint Bishop</description>
		<content:encoded><![CDATA[<p>Hey, the quick and easy solution is this:</p>
<p>new.rhtml:</p>
<p><code><br />
 { :action =&gt; 'create'} do |form| %&gt;</p>
<p>  Name<br />
   40  %&gt;</p>
<p>  Report Submitted on</p>
<p>    Report Line </p>
<p>  Comments<br />
   5, :cols =&gt; 60 %&gt;</p>
<p></code></p>
<p>expense_reports_controller.rb:</p>
<p><code><br />
class ExpenseReportsController "Create", "report"=&gt;{"expense_category_0"=&gt;"1", "submit_date(3i)"=&gt;"9",<br />
    # "submit_date(4i)"=&gt;"02", "expense_category_1"=&gt;"1", "submit_date(5i)"=&gt;"41",<br />
    # "comments"=&gt;"This was an awesome trip.", "amount_0"=&gt;"100", "expense_category_2"=&gt;"4",<br />
    # "project"=&gt;"Everybody party", "amount_1"=&gt;"200", "expense_category_3"=&gt;"2", "amount_2"=&gt;"300",<br />
    #     "expense_category_4"=&gt;"3", "amount_3"=&gt;"400", "amount_4"=&gt;"500", "submit_date(1i)"=&gt;"2007", "submit_date(2i)"=&gt;"6"}}<br />
    #<br />
    # nab the params you need based on the index (expense_category_x with amount_x) and save each.<br />
  end<br />
end<br />
</code></p>
<p>(pardon the possibly messy comment, I&#8217;m a little special when it comes to textile)</p>
<p>In short, I eschewed the collection_select thing in favor of just a plain ol&#8217; select that gets mapped in the controller action.  You&#8217;re going to need that to be populated with every expense_category you&#8217;ve got. I got rid of the fields_for stuff because the expense_categorIES instance variable doesn&#8217;t actually hang off of line_item.  Although you have to assign a single expense_categorY to the line_item, you&#8217;ll just need a select * of the categories table.</p>
<p>As to your two problems stated above, &#8216;line_items&#8217; won&#8217;t actually appear as a key in the params hash;  line_item_form will.  The best way to inspect your params is to just do</p>
<p>def create<br />
  raise params.inspect<br />
end</p>
<p>Or to use the TextMate Footnotes plugin, which displays all your state at the bottom of the page when in dev mode.</p>
<p>As to your second problem, you&#8217;re giving the text_field for amount the same name every time (:amount), so the form parameters for the five fields are being overwritten in series, since you&#8217;ll only be getting one :amount key in the params.  You&#8217;ll need to stick that id on the end to get unique names.</p>
<p>If it could be possible, it would be nice to somehow massage fields_for to break your parameters into sub-hashes to keep from having to do as much parsing of params, but I&#8217;m not sure how to do it without a little more prodding.  Anyway, HTH, fellow TWer.</p>
<p>Clint Bishop</p>
]]></content:encoded>
	</item>
</channel>
</rss>
