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

本文介绍了Rails中视图元素的使用方法,包括链接、图片、JavaScript和CSS文件的引入方式,以及如何通过yield和content_for进行模板内容的传递与渲染。此外还涉及了Rails的国际化配置和自动发现链接标签的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员和用户两个主要模块。管理员负责视频、文件和文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性和性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性和易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解和掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例和技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值