Rails 通过哈希查找条件

Event.find(
  :all,
  :conditions => [ "title like :search or description like :search",
                   {:search => "%Tiki%"}]
)

Rails 从Rails控制台调用ActionController方法

# terminal
script/server -u

# /some/view.erb
<% debugger %>

# open the url associated with that view, and switch over to the terminal to play with ActionController methods and objects
(rdb:2) request

Rails 计算Sql中的行数

Person.count(:conditions => "age > 26")

Rails 有用的遗留DB方法

class Sector < ActiveRecord::Base
  set_table_name "Sector"
  set_primary_key "SectorID"
end

Rails 突出显示部分导航

#put in application_helper.rb :

def section_link(name,options)
    if options[:action] == @current_action and options[:controller] == @current_controller
       link_to(name, options, :class => 'on')
    else
      link_to(name,options)
    end
end

#initialize vars in controller
before_filter :instantiate_controller_and_action_names

def instantiate_controller_and_action_names
      @current_action = action_name
      @current_controller = controller_name
end 

#usage in view:
<%=section_link('Home',:controller => 'articles', :action => 'index')%>

Rails will_paginate与jquery ajax /поÑ??Ñ,аничнÑ<йвÑ<воÐ'в轨道Ñ?? will_paginateÐJquery

//ajax function-------------------------------------

function ajax_will_paginate(s){
  $.ajax({
  type: &quot;POST&quot;,
  url: &quot;index/blog_paginate?page=&quot;+s,
  success: function(msg){
    $(&quot;div.blog[style='display: block;']&quot;).html(msg);
    replace_paginate_links();
  }
  });
}

// replace links function---------------------------

function replace_paginate_links(){
  $('div.pagination a').each(function(){
  var new_attr = &quot;ajax_will_paginate&quot;
  var get_href_a = $(this).attr('href').split('=');
  var get_href = get_href_a[get_href_a.length-1]

  $(this).attr('href',&quot;#&quot;)
  $(this).attr('onclick',&quot;ajax_will_paginate('&quot;+get_href+&quot;'); return false;&quot;)
})
}

$(document).ready(function(){
  replace_paginate_links()
})

Rails RoR - 用于连接表复选框的代码段

&lt;% for category in Category.find(:all) %&gt;
  &lt;%= check_box_tag &quot;post[category_ids][]&quot;, category.id, @post.categories.include?(category) %&gt;
  &lt;%= category.name %&gt;&lt;br /&gt;
&lt;% end %&gt;

Rails RoR - 与逗号分离的类别链接

&lt;% category_count = @post.categories.length %&gt;&lt;% count = 1 %&gt;
&lt;% @post.categories.each do |cat| %&gt;
  &lt;%= link_to cat.name, cat %&gt;&lt;%= &quot;, &quot; unless category_count == count %&gt;&lt;% count += 1 %&gt;
&lt;% end %&gt;

Rails RoR Helper - 如果为空则复制输入值

def name_copy(model)
    '&lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;
      var n = $(&quot;input#' + model + '_name&quot;);
      var t = $(&quot;input#' + model + '_title&quot;);
      var st = $(&quot;input#' + model + '_seo_title&quot;);
      var su = $(&quot;input#' + model + '_seo_url&quot;);
      
      n.blur(function(){
        if(t.val()==0){
          t.val(n.val());
        };
        
        if(st.val()==0){
          st.val(n.val());
        };
        
        if(su.val()==0){
          su.val(n.val());
        };
      });
    &lt;/script&gt;'
  end

Rails RoR Helper - 顶部菜单

def top_menu_li(label,controller,url)
    @current_page = request.request_uri
    @current_controller = request.path_parameters['controller']
    if @current_page == url || @current_controller == controller
      '&lt;li class=&quot;current&quot;&gt;&lt;a href=&quot;' + url + '&quot; title=&quot;' + label + '&quot;&gt;' + label + '&lt;/a&gt;&lt;/li&gt;'
    else
      '&lt;li&gt;&lt;a href=&quot;' + url + '&quot; title=&quot;' + label + '&quot;&gt;' + label + '&lt;/a&gt;&lt;/li&gt;'  
    end
  end