Cheapest viagra
Tuesday, May 19th, 2009
Update: thanks to Jon Wood aka jellybob, cheapest viagra, a prototype demonstration has been added, cheapest viagra, which is even better than my original jQuery btw as it degrades gracefully. Cheapest viagra, 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, cheapest viagra, and because everyone is tired of Rails dramas, cheapest viagra, I am risking that some of you won’t care to read the article - but I couldn’t resist :-). Cheapest viagra, Actually I’d like to talk about usage of (un)obtrusive Javascript - why is it a bad idea to be obtrusive, cheapest viagra, especially given that (as you will learn from the article) writing unobtrusive Javascript is not harder, cheapest viagra, and you get the warm, cheapest viagra, fuzzy feeling of writing nice and clean code!
The Drill
To demonstrate the differences, cheapest viagra, I’ll lead you through the creation of a quick AJAXy shout wall both the default/standard (and obtrusive) way, cheapest viagra, then do the same with unobtrusive Javascript to show you that contrary to the popular belief, cheapest viagra, you don’t need to memorize the “Tome of Javascript Black Magick Tricks” by heart, cheapest viagra, use obscure libraries or special coding techniques to achieve clean, cheapest viagra, unobtrusive code. Cheapest viagra, The shout wall is simply a form for posting a new message, cheapest viagra, and a list of messages below it, cheapest viagra, like so:
(You can check out the code used in this post from it’s github repository).
The Standard Way
Note: If you’d like to follow along, cheapest viagra, please use the provided pastie links - do not try to cut & paste multiple lines from the page (single lines are OK), cheapest viagra, as it will be b0rk3d.
-
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), cheapest 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, cheapest viagra, and fill it with the following content (Get it from pastie):
- <html>
- <head>
- <%= stylesheet_link_tag "application" %>
- <%= javascript_include_tag :defaults %>
- </head>
- <body>
- <%= yield %>
- </body>
- </html>
-
Create a file application.css and drop it into public/stylesheets. Cheapest viagra, Add the following content (Get it from pastie):
- body {
- background-color:#FFFFFF;
- color:#333333;
- font-family:"Lucida Grande", cheapest viagra,verdana, cheapest viagra,arial, cheapest viagra,helvetica, cheapest viagra,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, cheapest 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):
We are showing the form for the messages and list the already exiting messages below the list. Note that we are using the _remote_form_for_ Rails helper to create an AJAXy form. Cheapest viagra, This is already obtrusive, cheapest viagra, since if you observe the generated HTML, cheapest viagra, you will see that the form has an onsubmit parameter with some horribly looking code attached to it.:
- <h3 class="new-message-label">Enter new message!</h3>
- <% remote_form_for :message, cheapest viagra, :html => {:id => "message-form"} do |form| %>
- <dl>
- <dt>Author:</dt>
- <dd><%= text_field_tag ‘author’ %></dd>
- <dt>Message:</dt>
- <dd><%= text_area_tag ‘message’ %></dd>
- </dl>
- <%= submit_tag "Submit!", cheapest viagra, :id => "submit-button"%>
- <% end %>
- <div id="messages">
- <%= render :partial => ‘message’, cheapest viagra, :collection => @messages %>
- </div>
Sure, cheapest viagra, you can go ‘meh’ all the way, cheapest viagra, but slinging Javascript code all over the place is just as bad idea as writing inline CSS (or even worse, cheapest viagra, using HTML code for styling) or putting Rails code into views. Cheapest viagra, It will work without any problems - but it’s just not the right way of doing things, cheapest viagra, 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):
- <div class="message" id="message-<%=message.id%>">
- <div class="message-meta">on
- <%= message.created_at.to_formatted_s(:long_ordinal) %>, cheapest viagra,
- <span class="author"><%= message.author %></span>
- said:
- </div>
- <div><%= message.message %></div>
- </div>
-
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], cheapest viagra, :message => params[:message])
- end
-
And obviously we’ll need to render something to respond to the create action. Cheapest viagra, Using the standard Rails way, cheapest viagra, RJS, cheapest viagra, we might come up with something like this (in app/views/messages/create.js.rjs - Get it from pastie):
Here we insert the “messages” partial, cheapest viagra, using the just created @message, cheapest viagra, and throw a splash of yellow fade into the mix for good measure. Cheapest viagra, Easy peasy.
- page.insert_html :top, cheapest viagra, "messages", cheapest viagra, :partial => ‘message’, cheapest viagra, :object => @message
- page.visual_effect :highlight, cheapest viagra, "message-#{@message.id}"
- We are done! Fire up script/server, cheapest viagra, hit localhost:3000/messages and voila!
The Good Way
Here I am presenting only the steps that are different from the above - i.e. Cheapest viagra, if step 3 is skipped, cheapest viagra, use the one from above.
-
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, cheapest viagra, you can be just as unobtrusive with Prorotype -
but I switched to jQuery just before learning how, cheapest viagra, and now I am lazy to go backcheck out how in the ‘prototype unobtrusive’ directory in the github repository), cheapest viagra, you have to download jQuery with some basic effects, cheapest viagra, as well as an AJAX form handling library (still from the directory unobtrusive-shout-wall - Get it from pastie): - 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
- <%= javascript_include_tag :defaults %>
- <%= javascript_include_tag ‘jquery’ %>
- <%= javascript_include_tag ‘jquery-form’ %>
- <%= javascript_include_tag ‘application’ %>
- <%= javascript_include_tag ‘effects.core’ %>
- <%= javascript_include_tag ‘effects.highlight’ %>
- Same as above
- Same as above
- Same as above - just delete “remote” from the name of the helper, cheapest viagra, i.e. Cheapest viagra, use a standard Rails view helper, cheapest viagra, form_for
- Same as above
- Since we are not relying on Rails to do the rendering for as via a template file, cheapest viagra, we return the html chunk that we will render from Javascipt. Cheapest viagra, So your create action should look like (Get it from pastie):
- def create
- @message = Message.create(:author => params[:author], cheapest viagra, :message => params[:message])
- render :partial => ‘message’, cheapest viagra, :object => @message
- end
- Now comes the fundamentally different part - instead of using RJS to respond to the create action, cheapest viagra, we move all our code to application.js (Get if from pastie):
I don’t think so that this code is particularly more complicated or hard to understand that the RJS one. Cheapest viagra, Everything is inside the ready() function, cheapest viagra, which means that it’s only run once the document is properly loaded. Cheapest viagra, Then we declare that “#message-form” is an AJAX form, cheapest viagra, and that upon successful submission, cheapest viagra, the handleNewMessage() method should be called. Cheapest viagra, And if that happens, cheapest viagra, we add the response (which is the return value of the “create” action) to the “#messages” div, cheapest viagra, just as we did in RJS. Cheapest viagra, Then we apply the yellow fade! w00t!
- $(document).ready(function() {
- $("#message-form").ajaxForm({success: handleNewMessage});
- function handleNewMessage(response, cheapest viagra, statusText) {
- $("#messages").prepend(response).effect("highlight", cheapest viagra, {}, cheapest viagra, 1500);
- }
- });
- Same as above
(You can check out the code used in this post from it’s github repository).
Conclusion
As you can see, cheapest viagra, 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, cheapest viagra, we return the response as a string and dynamically insert it from jQuery. Cheapest viagra, With about the same effort, cheapest viagra, we kept all the Javascript code in application.js, cheapest viagra, which is much cleaner this way (you can open up 1 file and check out all the JS/AJAX behavior in one place), cheapest viagra, especially after introducing a lot of Javascript functionality into your code - in other words, cheapest viagra, for the same amount of work we got something much better. Cheapest viagra, Please try to keep this in mind when you are working with Javascript and Rails the next time - believe me, cheapest viagra, it can save you from a lot of pain!
Similar Posts:purchase viagra,cheapest viagra,buy xenical online,kamagra for sale,buy clonazepam without prescription

I have been living in India for 2 months last summer, cheap prozac, working on a Rails startup. Cheap prozac, Maybe I am odd or something, cheap prozac, but I knew that I had to remove my shoes when entering a Hindu temple, cheap prozac, and no one had to convince me (what’s more, cheap prozac, I didn’t even think about it for a second) wether this is the right thing to do, cheap prozac, why is it so, cheap prozac, whether I should do otherwise etc. Cheap prozac, This is a similar situation - I just don’t do X when speaking at a conference, cheap prozac, if I suspect that X makes feel even one person in the room uncomfortable, cheap prozac, whether because of his gender, cheap prozac, race, cheap prozac, nationality, cheap prozac, Ruby/Rails skills, cheap prozac, penis size or what have you - regardless whether I think it’s fine for me, cheap prozac, my wife, cheap prozac, for other members of the community and/or the majority of the room. Cheap prozac,
In
Update: Sergio, generic lexapro, the author of the livevalidation rails plugin updated the plugin so you can disregard the finale of the article (validatesconfirmationof is working, generic lexapro, as well as the newest version of livevalidation, generic lexapro, 1.3 is used in the plugin - so no additional tweaking is needed, generic lexapro, install and validate away ;-))










