Active Resource View Examples
Posted about 1 year 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:
<% form_tag(@model) do |f| -%>
<fieldset>
<legend>Company_Name Account Activation Form - Credit Card</legend>
<%# f.error_messages %>
<h2>Choose your Company_Name Plan </h2>
<p>
<%= label_tag :plan_id, "Company_Name Plan" %>
<%= select_tag 'model[plan_id]', options_from_collection_for_select(Plan.find(:all), 'id', 'name') %>
</p>
<h2>Billing Information</h2>
<p>
<%= label_tag "First Name: " %>
<%= text_field_tag "model[first_name]" %>
</p>
<p>
<%= label_tag :last_name, "Last Name: " %>
<%= text_field_tag "model[last_name]" %>
</p>
<h2>Credit Card Information</h2>
<p>
<%= label_tag :card_type, "Card Type: " %>
<%= select_tag "model[card_type]", options_for_select([ "VISA", "MasterCard" ], "MasterCard") %> <small>(Only Visa or Mastercard, sorry.)</small>
</p>
<p>
<%= label_tag :card_number, "Credit Card Number: " %>
<%= text_field_tag "model[card_number]", nil, :style => "width: 120px" %>
</p>
<p>
<%= label_tag :card_verification, "Card Verification Value(CVV): " %>
<%= text_field_tag "model[card_verification]", nil, :style => "width: 25px" %>
</p>
<p>
<%= label_tag :card_expires_on, "Credit Card Expiry Date: " %>
<%= select_month(Date.today, :add_month_numbers => true, :prefix => "model", :field_name => "card_month") %>
<%= select_year Date.today, :start_year => Date.today.year, :end_year => (Date.today.year+10), :prefix => "model", :field_name => "card_year" %>
</p>
<%= hidden_field_tag "model[ip_address]", :value => request.remote_ip %>
<p><%= submit_tag "Submit", :disable_with => 'Processing Please Wait...' %></p>
</fieldset>
<% end -%>
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
"For forskere, ett bestemt skala gir i rask kategorisering forbundet med en planetens relevans til biologi, i om noen samme måte som stellar typer umiddelbart kan fortelle en astronom noe om deres størrelse , temperatur, og som et resultat lysstyrke for stjerne "
Det er umulig i stedet for i markedet for å varsel hvorfor god du hund eller katten føler når du gnir under sine haken kanskje klø bak ørene. Vanligvis komfort bak hengivenhet hele konstant berøring og som et resultat kjærtegn oversettes til over alle lykke dessuten med mindre du er glad du ikke kan sikkert være sunn, kan du?
Recent Posts
Keeping Subscription dates tidy during February and Leap years
Testing ActiveResource - Basic Tutorial
Active Resource to Sinatra DataMapper backend
ArmRest and the tale of the No Schema Scheme
Textmate Ruby 1.9.1 and rvm - the facts
Easiest Postgres Install Ever - Mac Edition
Two Questions to Help Decide Between RDBMS, MongoDB or CouchDB
Testing Rails 3 - Just the Facts
Acts_as_snook - Creating an admin panel
Top 10 Movies of this Decade... Rebuttal
Thinking_Sphinx - Easy Setup Tutorial