Ruby on rails 开发中常用到的一些方法

ActiveViews:
1.link_to(image_tag()):
<%= link_to(image_tag('logo.png', :class=>'png left'), home_path) %>
Html 解析:<a href="/"><img alt="Logo" class="png left" src="http://try.lovdbyless.com/images/logo.png?1202945226" /></a>
2.导入Javscript 文件:
<%= javascript_include_tag :defaults, 'jquery', 'thickbox', 'truncator', :cache=>true %>
Html: <script src="http://try.lovdbyless.com/javascripts/all.js?1205716637" type="text/javascript"></script>
3.导入Css文件:
<%= stylesheet_link_tag 'reset', 'ie', 'application', :cache=>true %>
html: <link href="http://try.lovdbyless.com/stylesheets/all.css?1205716637" media="screen" rel="stylesheet" type="text/css" />
4.一个Issue:
<%= yield :head%>
例子:def fibUpTo(max)
i1, i2 = 1, 1 # parallel assignment
while i1 <= max
yield i1
i1, i2 = i2, i1+i2
end
end
fibUpTo(1000) { |f| print f, " " } #block
当执行到Yield语句的时候,会执行block中的内容,知道满足条件。
5.rails 国际化:globlize 插件:
[url]http://globalize.rubyforge.org/[/url]
class Product < ActiveRecord::Base
composed_of :price, :class_name => "Globalize::Currency",
:mapping => [ %w(price cents) ]
end

product.price -> "SFr. 483'232.43"

6.auto_discovery_link_tag(type = :rss, url_options = {}, tag_options = {})
Returns a link tag that browsers and news readers can use to auto-detect an RSS or ATOM feed. The type can either be :rss (default) or :atom. Control the link options in url_for format using the url_options. You can modify the LINK tag itself in tag_options.

Tag Options:

:rel - Specify the relation of this link, defaults to "alternate"
:type - Override the auto-generated mime type
:title - Specify the title of the link, defaults to the type

auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"}) # =>
<link rel="alternate" type="application/rss+xml" title="My RSS" href="http://www.curenthost.com/controller/feed" />
7.content_for(name, content = nil, &block)
Calling content_for stores the block of markup for later use. Subsequently, you can make calls to it by name with yield in another template or in the layout.

Example:

<% content_for("header") do %>
alert('hello world')
<% end %>
You can use yield :header anywhere in your templates.

<%= yield :header %>

8.textilize(text)
Returns the text with all the Textile codes turned into HTML tags. This method is only available if RedCloth is available.
9.sanitize(html)
Sanitizes the html by converting <form> and <script> tags into regular text, and removing all "onxxx" attributes (so that arbitrary Javascript cannot be executed). It also removes href= and src= attributes that start with "javascript:". You can modify what gets sanitized by defining VERBOTEN_TAGS and VERBOTEN_ATTRS before this Module is loaded.

sanitize('<script> do_nasty_stuff() </script>')
=> <script> do_nasty_stuff() </script>
sanitize('<a href="javascript: sucker();">Click here for $100</a>')
=> <a>Click here for $100</a>
阅读更多

没有更多推荐了,返回首页