`
52jobs
  • 浏览: 11513 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

rails view session layout

阅读更多
#208 erb-blocks
简介:在erb中使用blocks,可以为 html,封装逻辑

module ApplicationHelper
  def admin_area(&block)
    content_tag(:div, :class => "admin", &block) if admin?
  end
  
  def admin?
    true
  end
end

#234 simple_form
<%= simple_form_for @product do |f| %>
  <%= f.error_messages %>
  <%= f.input :name %>
  <%= f.input :price, :hint => "prices should be in USD" %>
  <%= f.input :released_on %>
  <%= f.association :category, :include_blank => false %>
  <%= f.input :rating, :collection => 1..5, :as => :radio %>
  <%= f.input :discontinued %>
  <%= f.button :submit %>
<% end %>



#132 helpers-outside-views
1、在 model 在使用helper
def description
  "This category has #{helpers.pluralize(products.count, 'product')}."
end

def helpers
  ActionController::Base.helpers
end
2、在controller中使用helper
flash[:notice] = "Successfully created #{@template.link_to('product', @product)}."


#7 layout
Layouts are view files that define the code that surrounds a template. They can be shared across many actions and controllers.
1、默认
全局 applicateion.html.erb
controller相关 layout project.html.erb
 
2、动态指定
class ProjectsController < ApplicationController
  layout :user_layout
  protected
   def user_layout
     if current_user.admin?
       "admin"
     else
       "application"
     end
   end
。。。
end 

3、直接指定
class ProjectsController < ApplicationController
  layout "admin"

  def index
    @projects = Project.find(:all)
    render :layout => 'projects'
  end
  
  render :layout => false
  
end






#125 dynamic-layouts
简介:实现动态布局


application.rb
def load_blog
  @current_blog = Blog.find_by_subdomain(current_subdomain)
  if @current_blog.nil?
    flash[:error] = "Blog invalid"
    redirect_to root_url
  else
    self.class.layout(@current_blog.layout_name || 'application')
  end
end
blogs_controller
1、使用application中的 load_blog
before_filter :load_blog, :only => :show

2、简单地在controller中指定
layout :blog_layout 
   
def blog_layout
"plain"    
@current_blog.layout_name
end

3、用户自定义
erviroment.rb
config.gem 'liquid'
custom.html.erb
<%= Liquid::Template.parse(@current_blog.custom_layout_content).
      render('page_content' => yield, 'page_title' => yield(:title)) 

     

# 8 Layouts and content_for
If you want to change something in the layout on a per-template basis, content_for is your answer! 
This allows templates to specify view code that can be placed anywhere in a layout.
通常,是template中的内容,通过yeild,填充到 layout中
也可以,tempate中的内容,能过content_for :title 转给 layout用,办法是 yield :title


example
template这个储存
<% content_for :head do %>
  <%= stylesheet_link_tag 'projects' %>
<% end %>
layouts中这个接收
yield :title



#30 Pretty Page Title
Using a Helper Method

module ApplicationHelper
  def title(page_title)
    content_for(:title) { page_title }
  end
end

template
<% title "Recent Episodes" %>

layout
<title><%= yield (:title) || "default page" %></title>





分享到:
评论

相关推荐

    rails_layout, 为各种前端框架生成 Rails 应用程序布局文件.zip

    rails_layout, 为各种前端框架生成 Rails 应用程序布局文件 RailsLayout gem使用这里 gem 可以设置你选择的前端框架的布局文件:Zurb基础 5.3Bootstrap 4.0Bootstrap 3.3它还将为 Bootstrap 或者基础设置设计视图。...

    rails-session_cookie:一个用于获取原始Rails会话Cookie的机架应用

    Rails :: SessionCookie 快速,松散耦合的请求有关经过Cookie验证的应用程序的规范。 为什么 可能,您可能已经看到了很多像这样的代码: # config/initializers/session_store.rb Rails . application . config . ...

    activerecord-session_store, 从 Rails 中提取的记录存储的活动会话.zip

    activerecord-session_store, 从 Rails 中提取的记录存储的活动会话 Active Record 会话存储由 Active Record 类支持的会话存储。 提供了默认类,但是任何对 Active Record 会话类的对象鸭类型都有文本 session_id ...

    activerecord-session_store:从Rails中提取的Active Record的会话存储

    rails generate active_record:session_migration 运行迁移: rake db:migrate 然后,在config/initializers/session_store.rb设置会话存储: Rails . application . config . session_store :active_record_...

    Rails 101 入门电子书

    - **作品**: 除了《Rails 101 入门电子书》外,xdite还编写了其他几本书籍,如《Maintainable Rails View》、《Lean SaaS》以及《Land Dream Rails Job》等。 - **特色**: 这本书是中文世界唯一一本基于Rails 4.0.0+...

    Rails项目源代码

    Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...

    rails 项目起步示例

    Rails是Ruby语言的一个著名Web开发框架,全称为Ruby on Rails,它遵循MVC(Model-View-Controller)架构模式,旨在提高开发效率和代码可读性。本示例"rails项目起步示例"是一个购物系统,非常适合初学者入门学习。 ...

    rails敏捷开发的购物车系统

    Rails提供了Session存储,可以用来临时存储用户的购物车信息,但这种存储方式不适用于持久保存。因此,通常我们会将购物车内容存入数据库,以便用户在不同会话之间保持购物车状态。在添加或删除商品时,更新购物车...

    rails2-sample

    此外,Rails还内置了许多实用功能,如ActiveRecord(用于数据库交互)、Action View(用于页面渲染)和Action Controller(用于处理用户请求),这些都使得开发者能够快速构建出功能丰富的Web应用程序。 #### 2. ...

    rails指南 中文版

    Ruby on Rails(简称Rails)是一个基于Ruby语言的开源Web应用框架,它遵循MVC(Model-View-Controller)架构模式,强调“约定优于配置”(Conventions over Configuration)和“Don't Repeat Yourself”(DRY,不要...

    Rails

    标题 "Rails" 指的是 Ruby on Rails,一个开源的Web应用程序框架,它基于Ruby编程语言,遵循MVC(模型-视图-控制器)架构模式。Rails由David Heinemeier Hansson在2004年创建,其设计理念是强调代码的简洁性、DRY...

    rails2.3.2

    Ruby on Rails(通常简称为 Rails)是一个基于 Ruby 语言的开源 Web 应用程序框架,它遵循 Model-View-Controller (MVC) 设计模式,用于构建数据库驱动的 Web 应用程序。Rails 强调“约定优于配置”(Convention ...

    关于rails 3.1 cucumber-rails 1.2.0

    Rails 3.1 和 Cucumber-Rails 1.2.0 是两个在Web开发领域非常重要的工具,尤其对于Ruby on Rails框架的测试和自动化流程。本文将深入探讨这两个组件,以及它们如何协同工作来增强软件开发的效率和质量。 首先,...

    rails 2.3.2离线安装rails 2.3.2离线安装

    rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...

    中文版rails教程

    在Ruby on Rails中,开发者可以快速构建功能丰富的动态网站,因为它提供了大量的内置功能和库,如数据库连接、ORM(对象关系映射)系统ActiveRecord、模板引擎ActionView以及路由系统ActionController等。...

    web开发之rails最新调试通过购物车代码

    5. **会话(Session)**:Rails中的会话管理允许在多个请求之间保持状态,这对于购物车尤为重要,因为用户可能需要在不同时间查看或修改购物车。 6. **数据库迁移(Database Migrations)**:Rails提供了方便的工具...

Global site tag (gtag.js) - Google Analytics