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

SKSsVH Can be also this issue because the truth can be achieved only in a dispute :D
This is really good information I have visited this blog to read something fresh and I really admire you efforts in doing so.
sxsypd
Various people in all countries take the mortgage loans in different creditors, just because it is simple.
Perhaps this is one of the most interesting blogs that I have ever seen. Interesting article, Funny comment. Keep it up!
Various programer not using these types of coding its obsolete now the season is for cloud based development php moved towards clouds and its a good opportunity for programmer to make php based cloud application.
Any suggestion regarding cloud based application develpement.
The facts connected with this post was really hot I am going to buy custom essay papers and men choose to buy term paper.
It will be not simple to make a correct option if you are going to buy non-plagiarized essay. You should be very meticulu making that!
That is unreal to get how to perform rss feed submission! Only professionals need to perform this stuff! I realize that true experts are hired at rss submission directories service. Link building occurs very popular at present time!
Your site gives me much interesting stuff here, I really enjoyed. I will be back for more new updates here.
Students need to write numbers of essays to understand the issue of the buying essays papers. But when students can not write, this would be much better to buy essay online. Then this will be possible to save time.
I do not will to have problems at the college. I I would better like to buy Essay Papers. all my comrades do that.
Do not make manual directory submissions automatically, just because you would possibly get a ban. Therefore, the manual directory submission service can do it in thoughtful manner.
That is really payless to have colorful articles related with this good topic. But really often some men turn on the PC and buy term papers and everybody could use the support of the experienced online custom writing service to buy an essay quickly.
You ought not to stack with your academic papers creating. That’s available to determine a helper such as get custom paper that will be ready to support you under any circumstances.
Its good article carrying good information. That’s what I were searching for.
I like your post. Been very informational. I am hoping you’ll keep up to date the nice work and keep the standard.
That is a smashing information associated with study. People dream about such kind of perfect writing skillfulness. But they must order the undergraduate dissertation suggested by some professional buy dissertation.
Cloud based development is in.. i think these Active Resources are a thing of past. Thank you for sharing the code example.
iQS4oO Not bad post, leave it at my bookmarks!….
 å hjelpe deg som eksperter hevder HIV-positive filippinere har vist seg å være tilstrekkelig beskyttet, eller oppfordret vår Institutt herunder Helse med Insurance Commission når du trenger å finne ute og farter hvorvidt forsikringsselskapene vil vært konstituert tvers henhold mens du bruker Republic Act 8504 pluss disse AIDS Prevention i tillegg til Kontroll Law.

"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?

9UPh4V comment3
oRWufa comment5
bhnmeoi
IBzpDq comment2
The author deserves for the monument:DD
Read, of course, far from my topic. But still, we can work together. How do you feel about trust management?!….
Post brought me to think, went to mull over!!….
It’s pleasant sitting at work to distract from it�to relax and read the information written here:DD
Good! Wish everybody wrote so:DD
The text is promising, will place the site to my favorites..!!
Stupid article..!
Post brought me to think, went to mull over!!…
Gripping! I would like to listen to the experts` views on the subject!!….
The topic is pretty complicated for a beginner!….
I was looking for the report in Yandex and suddenly came across this page. I found a little information on my topic of my report. I would like more, and thanks for that..!!
Left on my site a link to this post. I think many people will be interested in it..!!
Comrade kill yourself..
However, the author created a cool thing..!!
Yet, much is unclear. Could you describe in more details!….
Cool:) I would say say it exploded my brain..!!
Thank you very much! I took it for myself too. Will be useful!!…
Thank you, a very interesting note!!…
Thanks for all the answers:) In fact, learned a lot of new information. Dut I just didn`t figure out what is what till the end!….
Thanks for the article! I hope the author does not mind if I use it for my course work!….
Scribbler, give me a student’s record-book!))))
It’s straight to the point! You could not tell in other words! :DD
52. “The road will be overcome by that person, who goes.” I wish you never stopped and be creative – forever..!!
Thanks a lot! An extremely interesting comment!!…
The Author is crazy..!!
Can be also this issue because the truth can be achieved only in a dispute :DD
Uh, well, explain me a please, I am not quite in the subject, how can it be?!….
Yeah, in my opinion, it is written on every fence!!….
Hooray! the one who wrote is a cool guy..!!
I was looking for the report in Yandex and suddenly came across this page. I found a little information on my topic of my report. I would like more, and thanks for that..!!
I must admit, the webmaster is a cool guy..!!
Thanks:) Cool topic, write more often! You manage with it perfctly:DD
The text is promising, will place the site to my favorites..!!
Cool:) I would say say it exploded my brain..!!