• rails tips


    http://www.riverwatcher.com/web.html

    国内公司做国外的事


    http://net.tutsplus.com/tutorials/ruby/5-awesome-new-rails-3-features/

    5 Awesome New Rails 3 Features

        * Tutorials\
        * Ruby

    5 Awesome New Rails 3 Features
    John Gadbois on Sep 21st 2010 with 28 comments
    Tutorial Details

        *


        * Topic: Rails 3
        * Difficulty: N/A

    Tweet
    Share

    After more than a year of development, Ruby on Rails 3 was officially released to the public a few weeks ago. More than just an iterative update, this highly anticipated release was a major refactoring of the popular Ruby framework. Keep reading to learn five of the most awesome new features in Ruby Rails 3.
    1. Unobtrusive JavaScript

    One of my favorite new Ruby on Rails 3 features is the introduction of Unobtrusive JavaScript (UJS) to all of its JavaScript helper functions. In previous versions of Rails, JavaScript was generated inline with HTML, causing ugly and somewhat brittle code.

    As an example, Rails allows you to use its link_to method to generate a delete link for some object.
    view plaincopy to clipboardprint?

       1. <%= link_to "Delete this Post", @post, :confirm => "Do you really want to delete this post?", :method => :delete %> 

    Using this method in your view would generate the following in Rails 2:
    view plaincopy to clipboardprint?

       1. <a href="/posts/6" onclick="if (confirm('Do you really want to delete this post?')) { var f = document.createElement('form');        f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;        var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method');        m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;">Delete this Post</a> 

    Rails 3 would generate something much simpler:
    view plaincopy to clipboardprint?

       1. <a href='/posts/6"' rel="nofollow" data-method="delete" data-confirm="Do you really want to delete this post?">Delete this Post</a> 

        Rails 3 replaces all of the inline JavaScript with a couple of HTML5 attributes. All of the JavaScript event handlers to handle the actual confirmation box and deletion are stored in one central JavaScript file that is included with every rails project.

    One big advantage to this new method is that the JavaScript helpers are framework agnostic. Instead of being tied to the Prototype library like you were in Rails 2, you can now choose whatever JavaScript framework you like (Rails apps come with Prototype by default, but jQuery is now officially supported.
    2. Improved Security

    Another awesome new feature of Rails 3 is that XSS protection is now enabled by default. Rails 2 supported XSS protection through the use of the h method.
    view plaincopy to clipboardprint?

       1. <%= h @comment.text %> 

    The h method would escape html and JavaScript to ensure that no malicious client-side code was executed. This method worked great, but there was one problem: you had to actually remember to use theh method everywhere the user entered input was displayed. If you forgot even one place, then you were vulnerable to an XSS attack.

    In Rails 3, all input is escaped by default, taking the burden off of the developer of having to remember to escape everywhere that malicious code might be present. For those times that you do want to allow unescaped data to appear in your view, you can use the raw method to tell Rails 3 not to escape the data.
    view plaincopy to clipboardprint?

       1. <%= raw @comment.text %> 

    3. New Query Engine

    Rails 3 includes a cool new query engine that makes it easier to get back the data you want and gives you more flexibilitiy in your controller code. These changes show up in various places, but the most common case is fetching data in your controller. In Rails 2, you could use the find method to retrieve the data you were looking for, passing in arguments to specify conditions, grouping, limits, and any other query information. For example:
    view plaincopy to clipboardprint?

       1. @posts = Post.find(:all, :conditions => [ "category IN (?)", categories], :limit => 10, <img src="http://net.tutsplus.com/wp-includes/images/smilies/icon_surprised.gif" alt=":o" class="wp-smiley"> rder => "created_on DESC") 

    finds the first ten posts within some specified categories ordered by the creation time.

    In Rails 3, each of the passed in parameters has its own method, which can be chained together to get the same results.
    view plaincopy to clipboardprint?

       1. @posts = Post.where([ "category IN (?)", categories]).order("created_on DESC").limit(10) 

    The query is not actually executed until the data is needed; so these methods can even be used across multiple statements.
    view plaincopy to clipboardprint?

       1. @posts = Post.where([ "category IN (?)", categories]) 
       2. if(condition_a) 
       3.  @posts = @posts.where(['approved=?', true]) 
       4. else 
       5.  @posts = @posts.where(['approved=?', false]) 
       6. end 

    This is only a simple example, but should provide you with an idea of some of the ways this new syntax can be more useful.
    4. Easier Email

    The ActionMailer module has been rewritten to make it a lot easier for your application to send email in Rails 3. There are quite a few changes, but here are a couple of my favorites.
    1. Default Settings

    In Rails, a Mailer is a class that can have many methods, each of which generally configure and send an email. Previously, you had to set all of the parameters for each email separately in each method.
    view plaincopy to clipboardprint?

       1. class UserMailer < ActionMailer::Base 
       2.  
       3.  def welcome_email(user) 
       4.     from       "system@example.com" 
       5.  
       6.     # other paramters 
       7.  end 
       8.  
       9.  def password_reset(user) 
      10.     from       "system@example.com" 
      11.  
      12.     # other parameters 
      13.  
      14.  end 
      15.  
      16. end 

    In Rails 3, you can specify defaults that can be optionally overwritten in each method.
    view plaincopy to clipboardprint?

       1. class UserMailer < ActionMailer::Base 
       2.   default :from => 'no-reply@example.com',            :return_path => 'system@example.com' 
       3.  
       4.  def welcome_email(user) 
       5.     # no need to specify from parameter   end 
       6.  
       7. end 

    2. Cleaner APIs

    Previous versions of Rails required you to send email using special methods that were dynamically created by ActionMailer. For instance, if you wanted to deliver the welcome email in the example above, you would need to call:
    view plaincopy to clipboardprint?

       1. UserMailer.deliver_welcome_email(@user) 

    ln Rails 3, you can just call
    view plaincopy to clipboardprint?

       1. UserMailer.welcome_email(@user).deliver 

    This makes more sense semantically, and additionally allows you to retrieve and manipulate the Mail object before delivering the email.
    5. Dependency Management

    One of the strengths of the Ruby on Rails framework is the plethora of gems available for use by developers. Whether it's authentication, handling financial transactions, handling file uploads, or nearly anything else, chances are a gem exists to help with your problem.

    Issues can arise, however, if your gems require other gems, or developers are on different environments, among other things. To help solve these types of situations, Rails 3 adds the Bundler gem to help manage your dependencies. Using Bundler in Rails 3 is extremely simple; add a line for each gem you require in your Gemfile, a file included in the root of each of your applications.
    view plaincopy to clipboardprint?

       1. gem 'authlogic' 

    Once you've included all your gems, run:

       1. bundle install 

    and Bundler will download and configure all of the gems and their dependencies that you need for the project.

    Bundler also allows you to specify certain gems to only be configured in certain environments (development vs production vs testing).

    These are only a few of the many changes included in Ruby on Rails 3. Many of the old APIs still work in Rails, even if they've been deprecated, to make it easier to update. So, if you're on the fence about whether or not to upgrade your existing rails app, then go for it!

    Thanks for reading!

  • 相关阅读:
    [sql查询] 重复数据只取一条
    SSIS,参数坑
    数据仓库之建立多维数据库
    数据仓库之SSIS开发
    开发规范
    页面以base64输出图片
    内嵌iframe
    T-Sql编程基础
    MVC3.0----整理之一
    原生JS 表单提交验证器
  • 原文地址:https://www.cnblogs.com/lexus/p/1870873.html
Copyright © 2020-2023  润新知