May 19th, 2009

Update: thanks to Jon Wood aka jellybob Buy female pink viagra without prescription, , a prototype demonstration has been added, which is even better than my original jQuery btw as it degrades gracefully. Check it out in the 'prototype-unobtrusive' directory.
I am guessing 9 out of 10 of you reading the title is prepared for yet-another Rails drama on some obtrusive community members, Low price female pink viagra, and because everyone is tired of Rails dramas, I am risking that some of you won't care to read the article - but I couldn't resist :-). Actually I'd like to talk about usage of (un)obtrusive Javascript - why is it a bad idea to be obtrusive, female pink viagra tablet, especially given that (as you will learn from the article) writing unobtrusive Javascript is not harder, Female pink viagra online stores, and you get the warm, fuzzy feeling of writing nice and clean code.
The Drill
To demonstrate the differences, buy female pink viagra overnight delivery, I'll lead you through the creation of a quick AJAXy shout wall both the default/standard (and obtrusive) way, Approved female pink viagra pharmacy, then do the same with unobtrusive Javascript to show you that contrary to the popular belief, you don't need to memorize the "Tome of Javascript Black Magick Tricks" by heart, use obscure libraries or special coding techniques to achieve clean, buying female pink viagra online, unobtrusive code. Order female pink viagra in us, The shout wall is simply a form for posting a new message, and a list of messages below it, like so:

(You can check out the code used in this post from it's github repository), female pink viagra in us.
The Standard Way
Note: If you'd like to follow along, please use the provided pastie links - do not try to cut & paste multiple lines from the page (single lines are OK), as it will be b0rk3d, buy female pink viagra without prescription.
Creating a new Rails application
rails obtrusive-shout-wall
Get into the Rails dir
cd obtrusive-shout-wall
Generate the resource message
script/generate resource message
Add this the following to the generated migration (some_timestamp_create_messages (Get it from pastie):
t.string :author
t.text :message
Run the migrations:
rake db:migrate
Because we want to view the messages in reverse order (newest one first), Order female pink viagra, we add a default scope to the Message model (in message.rb):
default_scope :order => 'created_at DESC'
Create the application layout - create a new file in app/views/layouts called application.html.erb, and fill it with the following content (Get it from pastie):
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag :defaults %>
<%= yield %>
Create a file application.css and drop it into public/stylesheets. Add the following content (Get it from pastie):
body {
background-color:#FFFFFF;
color:#333333;
font-family:"Lucida Grande", purchase female pink viagra online,verdana, Drug female pink viagra, arial,helvetica,sans-serif;
margin:0 auto;
padding:0;
text-align:center;
width:960px;
}#messages {
text-align: left;
margin-left: 80px;
margin-top: 50px;
}#message-form {
text-align: left;
}#message-form dl {
margin:10px 0 0 80px;
}#message-form dd {
color:#666666;
font-size:11px;
line-height:24px;
margin:0 0 5px 80px;
}#message-form dt {
float:left;
font-size:14px;
line-height:24px;
width:80px;
text-align: left;
}#author {
margin-right: 640px;
}#message {
width: 600px;
height: 200px;
margin-right: 194px;
}.message {
margin-bottom: 20px;
}.first_row {
padding-bottom: 10px;
}.message-meta {
font-size: 12px;
}.author {
color: #FF5050;
font-weight: bold;
}.new-message-label {
text-align: left;
padding-top: 30px;
margin-left: 80px;
}#submit-button {
float : right;
margin-right: 195px;
margin-top: 10px;
}
Create a new action, find cheap female pink viagra, index in MessagesController (Get it from pastie):
def index
@messages = Message.all
end
This goes into app/views/messages/index.html.erb (Get it from pastie):
Enter new message!
<% remote_form_for :message, Generic female pink viagra, :html => {:id => "message-form"} do |form| %>
- Author:
- <%= text_field_tag 'author' %>
- Message:
- <%= text_area_tag 'message' %>
<%= submit_tag "Submit!", :id => "submit-button"%>
<% end %>
<%= render :partial => 'message', :collection => @messages %>
We are showing the form for the messages and list the already exiting messages below the list, cheap female pink viagra no rx.
Note that we are using the _remote_form_for_ Rails helper to create an AJAXy form. Find no rx female pink viagra, This is already obtrusive, since if you observe the generated HTML, you will see that the form has an onsubmit parameter with some horribly looking code attached to it.:

Sure, order female pink viagra from us, you can go 'meh' all the way, Cheapest female pink viagra price, but slinging Javascript code all over the place is just as bad idea as writing inline CSS (or even worse, using HTML code for styling) or putting Rails code into views. Buy female pink viagra without prescription, It will work without any problems - but it's just not the right way of doing things, especially if your code is going to hit a certain size.
You probably noticed that we are rendering a message as a partial - so create a partial file app/views/messages/_message.html.erb with the following content (Get it from pastie):
We need a 'create' action in MessagesController in order to process the form submission (Get it from pastie):
def create
@message = Message.create(:author => params[:author], Buy cheap female pink viagra internet, :message => params[:message])
end
And obviously we'll need to render something to respond to the create action. Using the standard Rails way, RJS, buy female pink viagra no rx, we might come up with something like this (in app/views/messages/create.js.rjs - Get it from pastie):
page.insert_html :top, Female pink viagra cheap price, "messages", :partial => 'message', :object => @message
page.visual_effect :highlight, female pink viagra free sample, "message-#{@message.id}"
Here we insert the "messages" partial, Buy cheap female pink viagra, using the just created @message, and throw a splash of yellow fade into the mix for good measure. Easy peasy, order female pink viagra on internet.
We are done. Fire up script/server, hit localhost:3000/messages and voila, buy female pink viagra without prescription. Order female pink viagra without prescription,
The Good Way
Here I am presenting only the steps that are different from the above - i.e. if step 3 is skipped, use the one from above, order discount female pink viagra.
Creating a new Rails application
rails unobtrusive-shout-wall
Get into the Rails dir
cd unobtrusive-shout-wall
- Same as above
- Same as above
- Same as above
- Same as above
- Since we are going to use jQuery (unobtrusiveness is *not* a property of jQuery, Female pink viagra without a prescription, you can be just as unobtrusive with Prorotype -
but I switched to jQuery just before learning how, and now I am lazy to go backcheck out how in the 'prototype unobtrusive' directory in the github repository), you have to download jQuery with some basic effects, find discount female pink viagra online, as well as an AJAX form handling library (still from the directory unobtrusive-shout-wall - Get it from pastie): - Same as above
- Same as above
- Same as above - just delete "remote" from the name of the helper, i.e. Buy female pink viagra without prescription, use a standard Rails view helper, form_for
- Same as above
- Since we are not relying on Rails to do the rendering for as via a template file, we return the html chunk that we will render from Javascipt. So your create action should look like (Get it from pastie):
def create
@message = Message.create(:author => params[:author], female pink viagra cheapest price, :message => params[:message])
render :partial => 'message', Where to order female pink viagra, :object => @message
end
- Now comes the fundamentally different part - instead of using RJS to respond to the create action, we move all our code to application.js (Get if from pastie):
$(document).ready(function() {
$("#message-form").ajaxForm({success: handleNewMessage});function handleNewMessage(response, statusText) {
$("#messages").prepend(response).effect("highlight", female pink viagra free delivery, {}, Buy female pink viagra lowest price, 1500);
}
});
I don't think so that this code is particularly more complicated or hard to understand that the RJS one. Everything is inside the ready() function, which means that it's only run once the document is properly loaded, female pink viagra online without prescription. Then we declare that "#message-form" is an AJAX form, Buy female pink viagra low price, and that upon successful submission, the handleNewMessage() method should be called. And if that happens, female pink viagra side effects, we add the response (which is the return value of the "create" action) to the "#messages" div, Female pink viagra online pharmacy, just as we did in RJS. Then we apply the yellow fade, buy female pink viagra without prescription. w00t.
- Same as above
curl http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js > public/javascripts/jquery.js
curl http://www.malsup.com/jquery/form/jquery.form.js?2.28 > public/javascripts/jquery-form.js
curl http://view.jquery.com/tags/ui/latest/ui/effects.core.js > public/javascripts/effects.core.js
curl http://view.jquery.com/tags/ui/latest/ui/effects.highlight.js > public/javascripts/effects.highlight.js
and replace
<%= javascript_include_tag :defaults %>
with
<%= javascript_include_tag 'jquery' %>
<%= javascript_include_tag 'jquery-form' %>
<%= javascript_include_tag 'application' %>
<%= javascript_include_tag 'effects.core' %>
<%= javascript_include_tag 'effects.highlight' %>
in the layout file. Female pink viagra without prescription,
(You can check out the code used in this post from it's github repository), female pink viagra medication.
Conclusion
As you can see, Female pink viagra online sales, the only real difference between the obtrusive and non-obtrusive version is in the last 2 points (downloading and including the jQuery header files can be easily solved with Rails templates): instead of leaving the rendering part to Rails, we return the response as a string and dynamically insert it from jQuery. With about the same effort, female pink viagra prices, we kept all the Javascript code in application.js, which is much cleaner this way (you can open up 1 file and check out all the JS/AJAX behavior in one place), especially after introducing a lot of Javascript functionality into your code - in other words, for the same amount of work we got something much better. Please try to keep this in mind when you are working with Javascript and Rails the next time - believe me, it can save you from a lot of pain.
Similar posts: Buy viagra without prescription. Buy gold viagra without prescription. Buy viagra super force without prescription. Buy viagra oral jelly without prescription. Buy viagra plus without prescription. Buy viagra professional without prescription. Buy viagra soft tabs without prescription. Buy viagra super active+ without prescription. Buy yagara (herbal viagra) without prescription. Buy black cialis without prescription.
Trackbacks from: Buy female pink viagra without prescription. Ultracet online without prescription. Buy folic acid (vitamin b9) without prescription. Buy depakote without prescription. Buy amaryl without prescription. Buy viagra soft tabs without prescription. Buy yagara (herbal viagra) without prescription. Betnovate without prescription. Buy aldactone without prescription. Buy nizoral without prescription.
Buy viagra without prescription, I surely don't have to introduce last week's Ruby/Rails earthquake to anyone by now - it has been covered by a boatload of blogs (including this one), analyzed, argued over, rebutted, reddited, dugg and whatnot - suffice to say, it's time to move on. It was a rather unpleasant drama piece, viagra pharmacy, Cheapest viagra prices, but fortunately it shed light on some problems the Ruby/Rails communities are facing, and (besides the ensuing trollfest and pointless arguing) it had a pleasant side-effect: a handful of people started to discuss how things could be made better, viagra buy drug, Tablet viagra, creating a small (and growing) but determined community: 
I have been living in India for 2 months last summer, Real gold viagra without prescription, working on a Rails startup. Maybe I am odd or something, but I knew that I had to remove my shoes when entering a Hindu temple, order gold viagra no prescription, and _no one had to convince me (what's more, Gold viagra online review, I didn't even think about it for a second) wether this is the right thing to do, why is it so, whether I should do otherwise etc_, buy gold viagra in canada. Buy gold viagra without prescription, This is a similar situation - I just don't do X when speaking at a conference, if I suspect that X makes feel even one person in the room uncomfortable, whether because of his gender, race, nationality, Ruby/Rails skills, penis size or what have you - _regardless whether I think it's fine for me, my wife, for other members of the community and/or the majority of the room_.


Buy viagra professional without prescription, I am sitting on the plane flying home from Dublin, trying to summarize my thoughts about the
Update: Buy viagra soft tabs without prescription, As several guys pointed out in the comments, Tim's remark which basically pulled the trigger was sarcastic - I guess have to re-calibrate my sarcasm meter. So you need to replace "Tim" and whatever he said with a different guy and his random quote. Viagra soft tabs rx, There are plenty of them out there these days, so the choice should be easy :-).
I spent the first 2+ years in Ruby-land without even knowing about the probably most underused (and underrated) keyword of the language: redo Buy yagara (herbal viagra) without prescription, . Buy yagara (herbal viagra) once daily, Even after I came across the first example and liked it immensely, I could not come up with another use for it, yagara (herbal viagra) prescription, Yagara (herbal viagra) malaysia, so I threw it to the bottom of my toolbox. Then I found another example, buying yagara (herbal viagra) online, Get yagara (herbal viagra), and another one - so I came to the conclusion that redo might be a valuable keyword in your Ruby arsenal after all - it is one of those things which you rarely need, but if you need it, order generic yagara (herbal viagra), Yagara (herbal viagra) free delivery, it's a perfect solution which would be cumbersome to replace with other constructs.
Buy black cialis without prescription, After running the Ruby Quiz show for almost a year, 




