iie labs

Active Resource View Examples

Posted over 4 years ago by Philip Ingram

It appears to me, that google only knows how to display Active Resource examples that use the Console to display the good stuff.

I’ve learned a lot from reading the Active Resource railscast 94 and Active Resource railscast 95 . I also read the chapter in the Rails in a Nutshell book . (Thanks to Cody Fauser, James MacAulay, Edward Ocampo-Gooding, and John Guenin, btw).

Each one of these sources used the console to show how slick this worked. Awesome, I get why they showed their work using the console too, but in the real world, we use views to get user input to an Active Resource.

The app i was creating needed a user to fill in a form and hit submit, wait, and get the response back. I wanted the model that was doing all the work to be on another server, so instantly we think of Active Resource to facilitate this workflow.

The Form Troubles begin… (OR if i knew then what i know now….)

When using Active Resource and forms to input the data that will be needed, you cannot use form_for; Instead, use form_tag made to act like form_for.

That one sentence above has saved you 10000000000 years of wtf. Your welcome :-)

From the api:

form_for – Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.

After seeing in countless resources "Active Resource just works like Active Record… " etc, etc I thought that i was dealing with a “Model Object” but I may have taken that point a little too literally. If you are reading this, then so have you I’m sure.

The fix with code examples

Again, let’s make a form_tag form look and act like a form_for. To the code:




	Company_Name Account Activation Form - Credit Card
	
	<h2>Choose your Company_Name Plan </h2>
	<p>
	
	
	</p>
	<h2>Billing Information</h2>
	<p>
	  
	  
	</p>
	<p>
	  
	  
	</p>
	
<h2>Credit Card Information</h2>
	<p>
	  
	    <small>(Only Visa or Mastercard, sorry.)</small>
	</p>
	<p>
	  
	   "width: 120px" %>
	</p>
	<p>
	  
	   "width: 25px" %>
	</p>
	<p>
	   
	 true, :prefix => "model", :field_name => "card_month")  %>
	   Date.today.year, :end_year => (Date.today.year+10), :prefix => "model", :field_name => "card_year"  %>
	</p>
	 request.remote_ip %>
	<p> 'Processing Please Wait...' %></p>
	

Summary of code example:

  • The whole purpose of doing this is so that we can replicate form_for and what it gives us. And that is the nice params Hash that will work great with your Active Resource Model.
  • The text_field_tag is pretty easy to understand, you just add “model[column_name]”as the second option
  • The select_tags, without dates, require you to set name attribute as you did with the text_field_tag and also you should use the options_from_collection_for_select inside the tag to compile your, well, select tag options.
    As a side note, I’m using another Active Resource called Plan as well, to find all the plan_id’s and names.
  • The key to the Date tags like select_month and select_year are:
     :prefix => “model”, :field_name => “card_year” 

    These two options will form the required name field and will be included in your params hash, as well.
    Lastly you need to add a before_validation to your ActiveRecord model to combine any dates to a Date object. You’ll be using this technique if you are dealing with Credit Cards.

Summary

Well that’s it for magic sauce i think. The rest comes down to how you validate, save data etc, in your Active Resource model.

Happy Active Resourcing.

Comments

blog comments powered by Disqus