header image

Archive for May, 2009

Cheapest viagra

Tuesday, May 19th, 2009

obtrusive_or_not.png 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:

shout_wall.png

(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.

  1. Creating a new Rails application
    1. rails obtrusive-shout-wall
  2. Get into the Rails dir
    1. cd obtrusive-shout-wall
  3. Generate the resource message
    1. script/generate resource message
  4. Add this the following to the generated migration (some_timestamp_create_messages (Get it from pastie):
    1. t.string :author
    2. t.text :message
  5. Run the migrations:
    1. rake db:migrate
  6. 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):
    1. default_scope :order => ‘created_at DESC’
  7. 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):
    1. <html>
    2.   <head>
    3.     <%= stylesheet_link_tag "application" %>
    4.         <%= javascript_include_tag :defaults %>     
    5.   </head>
    6.     <body>
    7.     <%= yield %>
    8.     </body>
    9. </html>
  8. Create a file application.css and drop it into public/stylesheets. Cheapest viagra, Add the following content (Get it from pastie):
    1. body {
    2.     background-color:#FFFFFF;
    3.     color:#333333;
    4.     font-family:"Lucida Grande", cheapest viagra,verdana, cheapest viagra,arial, cheapest viagra,helvetica, cheapest viagra,sans-serif;
    5.     margin:0 auto;
    6.     padding:0;
    7.     text-align:center;
    8.     width:960px;
    9. }
    10.  
    11. #messages {
    12.     text-align: left;
    13.     margin-left: 80px;
    14.     margin-top: 50px;
    15. }
    16.  
    17. #message-form {
    18.     text-align: left;
    19. }
    20.  
    21. #message-form dl {
    22.     margin:10px 0 0 80px;
    23. }
    24.  
    25. #message-form dd {
    26. color:#666666;
    27. font-size:11px;
    28. line-height:24px;
    29. margin:0 0 5px 80px;
    30. }
    31.  
    32. #message-form dt {
    33.     float:left;
    34.     font-size:14px;
    35.     line-height:24px;
    36.     width:80px;
    37.   text-align: left;
    38. }
    39.  
    40. #author {
    41.   margin-right: 640px;
    42. }
    43.  
    44. #message {
    45.   width: 600px;
    46.     height: 200px;
    47.   margin-right: 194px;
    48. }
    49.  
    50. .message {
    51.   margin-bottom: 20px;
    52. }
    53.  
    54. .first_row {
    55.   padding-bottom: 10px;
    56. }
    57.  
    58. .message-meta {
    59.     font-size: 12px;
    60. }
    61.  
    62. .author {
    63.   color: #FF5050;
    64.     font-weight: bold;
    65. }
    66.  
    67. .new-message-label {
    68.   text-align: left;
    69.   padding-top: 30px;
    70.   margin-left: 80px;
    71. }
    72.  
    73. #submit-button {
    74.   float : right;
    75.   margin-right: 195px;
    76.   margin-top: 10px;
    77. }
  9. Create a new action, cheapest viagra, index in MessagesController (Get it from pastie):
    1. def index
    2.   @messages = Message.all   
    3. end
  10. This goes into app/views/messages/index.html.erb (Get it from pastie):
    1. <h3 class="new-message-label">Enter new message!</h3>
    2. <% remote_form_for :message, cheapest viagra, :html => {:id => "message-form"} do |form| %>
    3.   <dl>
    4.         <dt>Author:</dt>
    5.     <dd><%= text_field_tag ‘author’ %></dd>
    6.         <dt>Message:</dt>
    7.         <dd><%= text_area_tag ‘message’ %></dd>
    8.     </dl>
    9.     <%= submit_tag "Submit!", cheapest viagra, :id => "submit-button"%>
    10. <% end %>
    11.  
    12. <div id="messages">
    13.     <%= render :partial => ‘message’, cheapest viagra, :collection => @messages %>
    14. </div>
    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.:

    Obtrusive helper.png

    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.
  11. 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):
    1. <div class="message" id="message-<%=message.id%>">
    2.   <div class="message-meta">on
    3.    <%= message.created_at.to_formatted_s(:long_ordinal) %>, cheapest viagra,
    4.    <span class="author"><%= message.author %></span>
    5.    said:
    6.   </div>
    7.   <div><%= message.message %></div>
    8. </div>
  12. We need a ‘create’ action in MessagesController in order to process the form submission (Get it from pastie):
    1. def create
    2.   @message = Message.create(:author => params[:author], cheapest viagra, :message => params[:message])
    3. end
  13. 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):
    1. page.insert_html :top, cheapest viagra, "messages", cheapest viagra, :partial => ‘message’, cheapest viagra, :object => @message
    2. page.visual_effect  :highlight, cheapest viagra, "message-#{@message.id}"
    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.
  14. 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.

  1. Creating a new Rails application
    1. rails unobtrusive-shout-wall
  2. Get into the Rails dir
    1. cd unobtrusive-shout-wall
  3. Same as above
  4. Same as above
  5. Same as above
  6. Same as above
  7. 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 back check 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):
    1. curl http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js > public/javascripts/jquery.js
    2. curl http://www.malsup.com/jquery/form/jquery.form.js?2.28 > public/javascripts/jquery-form.js
    3. curl http://view.jquery.com/tags/ui/latest/ui/effects.core.js > public/javascripts/effects.core.js
    4. curl http://view.jquery.com/tags/ui/latest/ui/effects.highlight.js > public/javascripts/effects.highlight.js
    and replace
    1. <%= javascript_include_tag :defaults %>
    with
    1. <%= javascript_include_tag ‘jquery’ %>     
    2. <%= javascript_include_tag ‘jquery-form’ %>
    3. <%= javascript_include_tag ‘application’ %>
    4. <%= javascript_include_tag ‘effects.core’ %>
    5. <%= javascript_include_tag ‘effects.highlight’ %>
    in the layout file.
  8. Same as above
  9. Same as above
  10. 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
  11. Same as above
  12. 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):
    1. def create
    2.   @message = Message.create(:author => params[:author], cheapest viagra, :message => params[:message])
    3.   render :partial => ‘message’, cheapest viagra, :object => @message
    4. end
  13. 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):
    1. $(document).ready(function() {     
    2.   $("#message-form").ajaxForm({success: handleNewMessage});
    3.  
    4.   function handleNewMessage(response, cheapest viagra, statusText) {
    5.     $("#messages").prepend(response).effect("highlight", cheapest viagra, {}, cheapest viagra, 1500);
    6.   }   
    7. });
    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!
  14. 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:zoloft pills,cheapest viagra,nexium sale,diazepam prescription,xanax online

Lexapro pills

Monday, May 4th, 2009

rails_bridge.pngI 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), lexapro pills, analyzed, lexapro pills, argued over, lexapro pills, rebutted, lexapro pills, reddited, lexapro pills, dugg and whatnot - suffice to say, lexapro pills, it’s time to move on. Lexapro pills, It was a rather unpleasant drama piece, lexapro pills, but fortunately it shed light on some problems the Ruby/Rails communities are facing, lexapro pills, 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, lexapro pills, creating a small (and growing) but determined community: RailsBridge!

Say Hello to RailsBridge!

RailsBridge’s mission is “To create an inclusive and friendly Ruby on Rails community” (check out details on the homepage). Lexapro pills, Everyone is welcome to participate - with ideas, lexapro pills, suggestions, lexapro pills, design, lexapro pills, code, lexapro pills, new project proposals or just about anything that would make Rails a more open and accepted technology and community. Lexapro pills,

It’s very important to understand that we are not trying to form yet another oh-my-god-we-are-so-awesome Rails group of uber-hackers which looks down on everyone not in it’s ranks - for two reasons, lexapro pills, at the very least:

  • RailsBridge is open to anyone! - you don’t have to be a Rails core commiter, lexapro pills, coder with 20 years of experience or a renowned Rails blogger - it’s enough if you’d like to make the Ruby/Rails scene more welcoming in any way
  • We are trying to promote the exact opposite philosophy - we acknowledge that Rails’s image is somewhat tarnished because of the “rock star” attitude and we’d like to show by our actions that we are working on this and will eventually turn Ruby and Rails into a very welcoming and enjoyable community

Surely, lexapro pills, this is quite a venture - our mission statement sounds great, lexapro pills, but everyone can do the talking on just about whichever Gordian Knot out there, lexapro pills, be it world hunger, lexapro pills, world peace or friendly Rails. Lexapro pills, We’ll have to demonstrate that we mean serious business, lexapro pills, and that’s why we need you - everyone’s ideas, lexapro pills, insights and help matters! Check out the RailsBridge homepage to learn more about what are we up to and how to get involved. Lexapro pills,

Similar Posts:zoloft sale,lexapro pills,viagra sale,tramadol online,purchase propecia online


Bad Behavior has blocked 1048 access attempts in the last 7 days.