`
squirrelRao
  • 浏览: 66836 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Rails content_for的使用

阅读更多

转载自:http://blog.tech4k.com/?tag=rails

 

Rails的Layout非常的好用,可以把Header,Footer,SideBar在一个公共的页面,统一风格而且非常的易于维护.

Layout中通常包含了的内容,而JQuery需要在header通过如下的code实现初始化.

<script type="text/javascript">
$(document).ready(function(){
......
}
</script>
 



而这部分代码肯定是根据具体的功能页面不同而有所不同的,无法直接写在Layout页面中,那该怎么办?


使用content_for
http://railscasts.com/episodes/8-layouts-and-content-for .
在Layout文件中加入如下的code.

<head>

<title>My Tasks</title>

<%= yield :jquery %>

</head>
 


在具体的功能页面中通过如下形式定义JQuery初始化脚本.

<% content_for :jquery do %>
<script type="text/javascript">
$(document).ready(function(){
......
}
</script>
<% end %>
 



OK了.这样就可以了.这个解决办法的问题是你必须在每个功能页面实现一边初始化脚本.另外如果header,sidebar需要用到jquery的话还需要额外处理.

 

 

在普通模板中使用content_for 方法就会给代码块中渲染的内容起一个名字,这些内容则会被 Rails 保存起来,不会被输出到模板生成的内容中。

分享到:
评论

相关推荐

    rails_content_issue:对 rails3 content_for 问题的调查

    content_for 或 capture 问题的演示。 导轨 3 导轨 3.2.17 Ruby 1.9.2-p320 神秘地清除 content_for 块 结果 # no js alerts. Layout yield :[removed] "" Layout yield :javascript2: "" ContentTest#example ...

    Bootstrap for Rails (2015)

    Fetching remote content from a Bootstrap modal 123 Using Bootstrap's modal in a Rails application 125 Summary 128 Chapter 9: Creating Image Slideshows with Bootstrap Carousel 129 Getting started with ...

    Learning Rails.pdf

    While most books written about Rails cater to programmers looking for information on data structures, Learning Rails targets web developers whose programming experience is tied directly to the Web. ...

    flutie:查看Rails应用程序的助手

    Flutie Flutie提供了一些实用程序视图帮助程序,可用于Rails应用程序。... “应用程序名称”来自您的应用程序创建的Rails应用程序的模块名称,即Appname::Application将产生“ Appname” “页面”来自尝试content_for(:

    液体导轨:渲染具有布局和部分支持的液体模板

    液体轨道 它允许您使用布局和部分支持来呈现.liquid模板。 它还提供了要在液体模板中使用的过滤器,标签,滴剂类。...{{ content_for_layout }} # It will render app/views/home/_partial.liquid wh

    render_async:render_async可让您使用AJAX异步包含页面

    = content_for :render_async %&gt;因此您可以选择将其放置在何处。 :megaphone: PS加入我们的进行帮助和讨论,让我们使render_async更好! :package: 安装 将此行添加到您的应用程序的Gemfile中: gem 'render_...

    fae:CMS for Rails。 真的

    费CMS for Rails。 真的。 与许多Rails CMS引擎一样,Fae提供了使您快速启动并运行的所有基础知识:身份验证,授权,时尚的UI,表单助手,图像处理和工作流。 但是与其他引擎不同,Fae生成的模型,控制器和视图是...

    where code and content meet

    that address the content-related aspects of custom software development for advanced websites or web platforms. So what are patterns? Patterns represent good practice and suggest solutions that have ...

    jQAPI - Alternative jQuery Documentation - For Version 1.7

    The alternative Rails documentation Rails API is my favorite documentation for my favorite server-side tool. Unfortunately my favorite client-side tool did not have such a fast and slick documentation...

    Startup-Discovery:寻找初创公司并关联他们的流量数据

    创业发现创建此应用程序是为了...将 morris.js javascript 文件移动到 show.html.erb(使用 content_for 方法)以改善页面加载时间在控制器中使用过时?(@company) 以避免在数据新鲜时执行额外的 API 调用(30 天内)

    famous-rails:Famo.us:heart_suit:滑轨

    著名铁路Famo.us for Rails资产管道警告! 由于资产管道和RequireJS之间的冲突,当在生产模式下使用时,此gem可能会有几个问题。 但它(应该)在开发人员模式下运行良好。 由于Famo.us推广了其全新的 ,该与旧的Famo...

    Ric.Shreves.PDF文字版.pdf

    is an open-source content management system (CMS) for Web sites. While it is free and relatively easy to use, there are lots of tricks and functionality that may not be intuitive to new users or ...

    comfortable-mexican-sofa.zip

    ComfortableMexicanSofa 是个强大的... Fixtures for initial content population Revision History Great extendable admin interface built with Bootstrap, CodeMirror and WYSIHTML5 标签:comfortable

    Programming Ruby 1.9 & 2.0 The Pragmatic Programmers’ Guide 4th Edition

    Are you using Rails, and want to dig deeper into the underlying Ruby language? If so, then we’ve got a language and book for you! Ruby is a fully object-oriented language, much like the classic ...

    Advanced Flex 3 2008

    approached independent of the rest of the content in this book. Bare essential knowledge of Flex is assumed of the readers. How this book is structured Advanced Flex 3 is divided into 3 parts that ...

    让gedit支持rhtml,html.erb高亮显示用到的两个文件

    This method works for me. It's taken from this article. Unfortunately the commands showed there doesn't work because the files that we're supposed to download are not available. So here are the ...

    JAVA上百实例源码以及开源项目

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

    JAVA上百实例源码以及开源项目源代码

    2个目标文件,FTP的目标是:(1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户...

Global site tag (gtag.js) - Google Analytics