`
yang_kunlun
  • 浏览: 74925 次
  • 性别: Icon_minigender_1
  • 来自: 地球
最近访客 更多访客>>
社区版块
存档分类
最新评论

Rails view

    博客分类:
  • ROR
阅读更多
从文本字段读取数据
输入视图:
<form action = "\look\at">
...
  <input type="text" name="text1">
...
</form>
动作:
def at
  @date = params[:text1]
end
输出视图:
<br>
Your Name is <% = @date %>
</br>

从复选框读取数据
输入视图:
<form action = "\look\at">
..
  <input type="checkbox" name="check1">
..
</form>
动作:
def at
  @date = params[:check1]
end
输出视图:
    <br>
    <% if @data %>
    You clicked yes.
    <% else %>
    You did not click yes.
    <% end %>
    <br>

从单选按钮中读取数据
输入视图:
<form action="\look\at">
...
  <input type="radio" name="radio1" value="red">red
  <input type="radio" name="radio1" value="blue">blue
  <input type="radio" name="radio1" value="green">green
...
</fomr>
动作:
  def at
    @data = params[:radios1]
  end
输出视图:
    <br>
    You selected <%= @data %>.
    <br>

从选择框读取数据
输入视图:
    <form action = "\look\at">
      Select your new car's color.
      <br>
      <select name="select1" >
        <option value="red">red
        <option value="green">green
        <option value="blue">blue
      </select>
      <br>
      <br>
      <input type="submit"/>
    </form>
动作:
  def at
    @data = params[:select1]
  end
输出视图:
<br>
Your Select <% = @date %>
</br>

从选择框读取多个选择
输入视图:
    <form action = "\look\at">
      Select your new car's color.
      <br>
      <select name="select1[]" multiple size="3">
        <option value="red">red
        <option value="green">green
        <option value="blue">blue
      </select>
      <br>
      <br>
      <input type="submit"/>
    </form>
动作相同
输出视图:
    <br>
    You selected <% for data in @data %>
    <%= data %>
    <% end %>
    <br>

Rails中使用HTML的快捷方法
输入视图:
    <%= start_form_tag ({:action => "at"}, {:method => "post"}) %>
      Please enter your name.
      <br>
      <%= text_field_tag ("text1", "", {"size" => 30}) %>
      <br>
      <br>
      <input type="submit"/>
    <%= end_form_tag %>
动作:
  def at
    @data = params[:text1]
  end
  def input
  end
新增加了一个input方法,所以上面的视图是保存在以.rhtml结尾的文件中
输出视图:
    <br>
    Your name is <%= @data %>.
    <br>
给start_form_tag 方法传递了一个指定动作的散列表,以及一个可选的散列表选项--有两个选项:"method"可以是"get"或"post", ":multipart"可以是"true"或"false"
使用text_field_tag 方法创建文本字段,需要将文本字段的名称,在文本字段中显示的初始文本以及一个可选的用于设置标准HTML选项--:disable,:maxsize或:size--的散列表传递给这个方法
创建复选框用check_box_tag方法,创建单选按钮使用radio_button_tag方法。
创建check_box:
    <%= start_form_tag ({:action => "at"}, {:method => "post"}) %>
      Would you like a raise?
      <br>
      <br>
      <%= check_box_tag ("check1", "Yes", false) %>Yes
      <br>
      <br>
      <input type="submit"/>
    <%= end_form_tag %>
创建select:
    <%= start_form_tag ({:action => "at"}, {:method => "post"}) %>
      Select your new car's color.
      <br>
      <br>
      <%= select_tag ("select1[]", "<option value='red'>red<option value='green'>green<option
value='blue'>blue", {:multiple => true}) %>
      <br>
      <br>
      <input type="submit"/>
    <%= end_form_tag %>

使用模型:
只需要在应用程序的module目录下放置一个.rb文件就能创建模型。
使用模型:在动作中创建一个模型的对象,使用这个对象的方法。
模型的代码:
class Cruncher
  def crunch
    return 5
  end
end
使用模型:
  def at
    @cruncher = Cruncher.new
    @data = @cruncher.crunch
  end

绑定控件和模型
rails支持一系列方法--text_field,select,check_box,可以使用这些方法直接将HTML控件连接到模型。
text_field(object_name, method, {...})
object_name是模型对象的名字,method是需要与之绑定的控件的数据访问器名称,{...}是一个包含有这个文本字段的HTML设置的散列表,如{"size" => 30}
模型:
class Cruncher
  attr_reader :crunch
  attr_writer :crunch
  def initialize(data)
    @crunch = data
  end
end
动作:
  def at
    @data_hash = params[:cruncher]
    @cruncher = Cruncher.new(@data_hash[:crunch])
    @data = @cruncher.crunch
  end
  def input
  end
输入视图:
    <%= start_form_tag ({:action => "at"}, {:method => "post"}) %>
      Please enter your name.
      <br>
      <%= text_field ("cruncher", "crunch", {"size" => 30}) %>
      <br>
      <br>
      <input type="submit"/>
    <%= end_form_tag %>
输出视图:
    <br>
    Your name is <%= @data %>.
    <br>
在视图中使用cruncher对象将文本字符中的数据绑定到这个crunch属性。
文本字符中的数据会在以:cruncher为参数的散列表中传回。
初始化控件中的数据
只需要在动作中,修改input动作:
  def input
    @cuncher = Cruncher.crunch
  end

存储会话中的数据
  session[:data] = @data
分享到:
评论

相关推荐

    Maintainable Rails View

    Rails View Best Practices

    Learning Rails 5(高清文字pdf版)

    You’ll learn how to create something visible with Rails’ view layer before diving into the more difficult inner layers: the database models and controller code. All you need to begin your Rails ...

    Rails之道,完整扫描版

    分别介绍了Rails的环境、初始过程、配置和日志记录,Rails的分配器、控制器、页面生成和路由,REST、资源和Rails,ActiveRecord的基础、关联、验证和高级技巧,ActionView的模板、缓存和帮助器,Ajax、Prototype和...

    提升Ruby on Rails性能的几个解决方案

    简介 Ruby On Rails 框架自它提出之日...Rails 是一个真正彻底的 MVC(Model-View-Controller) 框架,Rails 清楚地将你的模型的代码与你的控制器的应用逻辑从 View 代码中分离出来。Rails 开发人员很少或者可能从未遇到

    rails, Ruby on Rails.zip

    rails, Ruby on Rails 欢迎使用 RailsRails 是一个web应用程序框架,它包括根据 Model-View-Controller ( MVC ) Pattern 创建数据库备份的web应用程序所需的所有内容。理解 MVC Pattern 是理解 Rai

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    With this fully revised new edition, take a holistic view of full-stack development to create usable, high-performing applications with Rails 5. Rails is a great tool for building web applications, ...

    Rails 4 in Action(Manning,2015)

    You'll find no substitute for the guru's-eye-view of design, testing, deployment, and other real-world concerns that this book provides. Rails 4 in Action is a hands-on guide to the subject. In this...

    Rails的精简版本Rails::API.zip

    Rails::API 移除了 ActionView 和其他一些渲染功能,不关心Web前端的开发者可更容易、快速地开发应用程序,因此运行速度比正常的 Rails 应用程序要快。 Rails::API 可以用来创建只提供API服务(API-Only)的 Rails ...

    Learning Rails.pdf

    Use Rails scaffolding to build applications from a view-centric perspective • Add common web application elements such as sessions, cookies, and authentication • Build applications that combine ...

    Rails管理框架upmin-admin.zip

    upmin-admin 是一个为 Rails 应用开发的开源管理框架。用来管理 Rails 应用中各种对象(如 Model、View 和 Controller )。 标签:upmin

    Learning Rails 5(高清文字epub版)

    You’ll learn how to create something visible with Rails’ view layer before diving into the more difficult inner layers: the database models and controller code. All you need to begin your Rails ...

    Learning Rails 5(高清文字kindle版)

    You’ll learn how to create something visible with Rails’ view layer before diving into the more difficult inner layers: the database models and controller code. All you need to begin your Rails ...

    张文钿 Rails Best Practices 幻灯片

    View * Move code into controller * Move code into helper * Replace instance variable with local variable Ruby5 不久前在2009年11月17日的Podcast中介绍了rails_best_practices,而railscasts.com的Ryan...

    论文研究-Ruby on Rails的性能调优方案研究 .pdf

    Ruby on Rails的性能调优方案研究,张淼森,杨杰,Ruby on Rails 框架自它提出之日起就受到广泛关注。由于Rails框架基于MVC(Model-View-Controller) 模型,可以清楚地将模型层的代码与控制层的应

    web开发_ruby_on_rails

    准的编程模式,比如ActiveRecord 以及Model-View-Controller。它重用很多已经存在 的Ruby 库(比如Rake和ERb)。Rails 的功能在于集成这些标准的技术自动化完成任务, 并且会断言合理的默认行为。

    rails-settings:使用Ruby on Rails管理设置

    Rails的设置 Ruby gem通过将...class User &lt; ActiveRecord xss=removed&gt; { :theme =&gt; 'blue' , :view =&gt; 'monthly' , :filter =&gt; false } s . key :calendar , :defaults =&gt; { :scope =&gt; 'company' } end end

    rails-dom-testing, 从ActionView中提取DomAssertions和 SelectorAssertions.zip

    rails-dom-testing, 从ActionView中提取DomAssertions和 SelectorAssertions 命令行:::: dom:: 测试这里 gem 负责比较 HTML in,并断言在 Rails 应用程序中存在DOM元素。 通过 assert_dom_equal 和 assert_dom_not_...

    Enterprise Rails

    * Base an ActiveRecord model on a database view, and build support for multiple table inheritance * Explore service-oriented architecture and web services with XML-RPC and REST * See how caching ...

Global site tag (gtag.js) - Google Analytics