Ruby on Rails 之旅(七)—— Ruby on Rails 入门(2)

本文介绍如何使用 Ruby on Rails 框架实现一个完整的 CRUD 应用,包括模型定义、控制器编写、视图展示及验证机制的实现。

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





Active Record objects don‘t specify their attributes directly, but rather infer them from the table definition with which they‘re linked. Adding, removing, and changing attributes and their type is done directly in the database. Any change is instantly reflected in the Active Record objects. The mapping that binds a given Active Record class to a certain database table will happen automatically in most common cases, but can be overwritten for the uncommon ones.

本章就来学习一下ROR的特色的ActiveRecord

用下面语句创建一个model名字叫event,具有name,description,is_public,capacity属性。

script/generate model event name:string description:text is_public:boolean capacity:integer

接下来创建表

rake db:migrate

查看一下DB中是否成功创建event表

接下来用一组命令语句来展示一下(script/console):

>> event = Event.newevent = Event.new=> #<Event id: nil, name: nil, description: nil, is_public: nil, capacity: nil, created_at: nil, updated_at: nil>>> event.name = "Ruby course"event.name = "Ruby course"=> "Ruby course">> event.description = "fooobarrrrr"event.description = "fooobarrrrr"=> "fooobarrrrr">> event.saveevent.save=> true>> event.idevent.id=> 1>> e = Event.find(1)e = Event.find(1)=> #<Event id: 1, name: "Ruby course", description: "fooobarrrrr", is_public: nil, capacity: nil, created_at: "2010-08-11 07:42:15", updated_at: "2010-08-11 07:42:15">>> e = Event.find_by_name("Ruby course")e = Event.find_by_name("Ruby course")=> #<Event id: 1, name: "Ruby course", description: "fooobarrrrr", is_public: nil, capacity: nil, created_at: "2010-08-11 07:42:15", updated_at: "2010-08-11 07:42:15">>> e = Event.find(:first, :conditions => { :name => "Ruby course" })e = Event.find(:first, :conditions => { :name => "Ruby course" })=> #<Event id: 1, name: "Ruby course", description: "fooobarrrrr", is_public: nil, capacity: nil, created_at: "2010-08-11 07:42:15", updated_at: "2010-08-11 07:42:15">>> e = Event.find(:first, :conditions => ["name = ?", "Ruby course"])e = Event.find(:first, :conditions => ["name = ?", "Ruby course"])=> #<Event id: 1, name: "Ruby course", description: "fooobarrrrr", is_public: nil, capacity: nil, created_at: "2010-08-11 07:42:15", updated_at: "2010-08-11 07:42:15">>> e.namee.name=> "Ruby course">> e.update_attributes( :name => 'abc', :is_public => false )e.update_attributes( :name => 'abc', :is_public => false )=> true>> e.destroye.destroy=> #<Event id: 1, name: "abc", description: "fooobarrrrr", is_public: false, capacity: nil, created_at: "2010-08-11 07:42:15", updated_at: "2010-08-11 07:43:01">>> Event.find(:all, :limit => 2, :order => 'id DESC')Event.find(:all, :limit => 2, :order => 'id DESC')=> []>> 

注意观察过程,及语法特点。

model的基本应用学会之后,我们来学一下Model Validation

编辑:/app/models/event.rb

class Event < ActiveRecord::Base    validates_presence_of :nameend

离开 console 重新进入,或者执行 reload!

>> event = Event.newevent = Event.new=> #<Event id: nil, name: nil, description: nil, is_public: nil, capacity: nil, created_at: nil, updated_at: nil>>> event.saveevent.save=> false>> event.errors.full_messagesevent.errors.full_messages=> ["Name can't be blank"]>> event.name = 'ihower'event.name = 'ihower'=> "ihower">> event.saveevent.save=> true>> 

看来,我们刚才追加的Validation已经生效了。

接下来不适用脚手架来创建一套CRUD应用。

script/generate controller events

编辑文件:app/controllers/events_controller.rb

  def index    @events = Event.find(:all)  end   def show    @event = Event.find(params[:id])  end    def new    @event = Event.new  end    def create    @event = Event.new(params[:event])    @event.save        redirect_to :action => :index  end    def edit    @event = Event.find(params[:id])  end    def update    @event = Event.find(params[:id])    @event.update_attributes(params[:event])        redirect_to :action => :show, :id => @event  end    def destroy    @event = Event.find(params[:id])    @event.destroy        redirect_to :action => :index  end

新增:/app/views/events/index.html.erb

    <ul>      <% @events.each do |event| %>      <li>        <%= link_to event.name, :controller => 'events',        :action => 'show', :id => event %>        <%= link_to 'edit', :controller => 'events',        :action => 'edit', :id => event %>        <%= link_to 'delete', :controller => 'events',        :action => 'destroy', :id => event %>      </li>      <% end -%>    </ul>    <%= link_to 'new event', :controller => 'events', :action => 'new' %>

新增:/app/views/events/show.html.erb

    <%=h @event.name %>    <%=h @event.description %>    <p>      <%= link_to 'back to index', :controller => 'events',      :action => 'index' %>    </p>

新增:/app/views/events/new.html.erb

    <% form_for @event, :url => { :controller => 'events',    :action => 'create' } do |f| %>    <%= f.label :name, "Name" %>    <%= f.text_field :name %>    <%= f.label :description, "Description" %>    <%= f.text_area :description %>    <%= f.submit "Create" %>    <% end %>

新建:/app/views/events/edit.html.erb

<% form_for @event, :url => { :controller => 'events',:action => 'update', :id => @event } do |f| %><%= f.label :name, "Name" %><%= f.text_field :name %><%= f.label :description, "Description" %><%= f.text_area :description %><%= f.submit "Update" %><% end %>

 

访问网址:http://localhost:3000/events

看看是不是已经看到没有任何样式的画面了。

画面样式如何加呢?每个画面引入?太老土了。看看ROR的做法:

新建:/app/views/layout/application.html.erb

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>  <meta http-equiv="content-type"        content="text/html;charset=UTF-8" />  <title>Posts: <%= controller.action_name %></title>  <%= stylesheet_link_tag 'scaffold' %></head><body style="background: #EEEEEE;"><p style="color: green"><%= flash[:notice] %></p><%= yield  %></body></html>

在刷新一下画面,呵呵~是不是变成灰色背景了?这说明已经剩下了。

如果你还有其他的controller可以看看是不是都变色了。

这个是全局设置。如果想知识应用到events怎么做呢?

很简单,创建的文件名application.html.erb->events.html.erb

再看看,是不是只有events下的文件生效了呢。

很方便吧。


接下来看看flash

  1. 在 create action 中加入 flash[:notice] = "创建成功"
  2. 在 update action 中加入 flash[:notice] = "修改成功"
  3. 在 destroy action 中加入 flash[:notice] = "删除成功"

 

然后用画面跑一边所有应用,是不是对应的成功消息都打印出来了。

下面来看看部分模板(Partial template)的使用方法:

 new.html.erb  edit.html.erb 的窗体程序移往 _form.html.erb,内容如下

<%= f.label :name, "Name" %><%= f.text_field :name %><%= f.label :description, "Description" %><%= f.text_area :description %>

 new.html.erb  edit.html.erb 的窗体内加回

<%= render :partial => 'form', :locals => { :f => f } %>

再跑一圈画面,应该还是跟之前一样的,说明编辑和创建画面已经成功的应用了模板文件了。

 

再来看看before_filter

在 events_controller.rb 中加入:

before_filter :find_event, :only => [ :show, :edit, :update, :destroy]

 

 

创建下面方法:

  protected  def find_event    @event = Event.find(params[:id])  end 

删除 show, edit, update, destroy 方法中的 @event = Event.find(params[:id])

这样就达到了在执行show, edit, update, destroy等几个方法前都共同调用了find_event方法,并把值保存在实例变量中。

 

下面接续学习Model validation

编辑 /app/models/event.rb 加入 

 

validates_presence_of :name

修改/app/controllers/events.rb 的 create 和 update 方法

  def create    @event = Event.new(params[:event])        if @event.save      flash[:notice] = "创建成功"      redirect_to :action => :index    else      render :action => :new    end      end  def update        if @event.update_attributes(params[:event])      flash[:notice] = "修改成功"      redirect_to :action => :show, :id => @event    else      render :action => :edit    end        end

编辑 new.html.erb  edit.html.erb 加入

<%= error_messages_for :event %>

 

回到画面上,在编辑画面或者新建画面中不输入名字,然后点保存,会看到下面的样子:

说明生效了。

常用的功能还有分页,让我们组装一下:

script/plugin install git://github.com/mislav/will_paginate

windows下默认没有安装git,到这里下载:http://code.google.com/p/msysgit/downloads/list

然后就可以使用上面的语句安装了。

安装完之后,修改 /app/controllers/events_controller.rb 的 index 方法如下:

 

def index @events = Event.paginate(:page => params[:page], :per_page => 3)end

編輯 /app/views/events/index.html.erb,循环外加入

<%= will_paginate @events %>

添加超过3天数据看看,是不是出现翻页了。

 

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值