Rails开发流程
程序员文章站
2022-06-12 18:17:43
...
1. 画出页面流(Page Flow)
2. 画出要处理的数据(Data)草图
3. 与客户达成共识
4. 开始(创建deopt应用)
5. 创建products表对应的模型、试图、控制器和迁移任务。
6. 让Rails把这个迁移任务实施到开发数据库上
7. 启动测试服务器
8. 打开浏览器,看看效果:
点击New Product
9. 添加缺失字段:(price)
编辑文件:
编辑后的代码为:
10. 再次运行数据迁移
11. 上步执行后,我们仅仅处理了模型,控制的流程并没有改变,也就是说控制器不需要改变,需要改变的是视图,我们还需要修改四个文件:
11.1 C:\Users\Tony\Desktop\rails\deopt\app\views\products\index.html.erb
11.2 C:\Users\Tony\Desktop\rails\deopt\app\views\products\new.html.erb
11.3 C:\Users\Tony\Desktop\rails\deopt\app\views\products\edit.html.erb
11.4 C:\Users\Tony\Desktop\rails\deopt\app\views\products\show.html.erb
12. 点击New Product后页面最上方显示了这样的错误:error_messages_for was removed from Rails and is now available as a plugin
需要做的一些修改:
12.1 在 C:\Users\Tony\Desktop\rails\deopt\script下执行下面命令:
C:\Users\Tony\Desktop\rails\deopt\script>rails plugin install git://github.com/rails/dynamic_form.git
12.2 修改文件:C:\Users\Tony\Desktop\rails\deopt\Gemfile
添加一行: gem 'dynamic_form'
修改后的代码:
12.3 执行
C:\Users\Tony\Desktop\rails\deopt>bundle install
12.4 可以使用:
C:\Users\Tony\Desktop\rails\deopt>bundle show
查看所有gems的bundle。
2. 画出要处理的数据(Data)草图
3. 与客户达成共识
4. 开始(创建deopt应用)
C:\Users\Tony\Desktop\rails>rails new deopt
5. 创建products表对应的模型、试图、控制器和迁移任务。
C:\Users\Tony\Desktop\rails\deopt>rails generate scaffold product title:string description:text image_url:string
6. 让Rails把这个迁移任务实施到开发数据库上
C:\Users\Tony\Desktop\rails\deopt>rake db:migrate
7. 启动测试服务器
C:\Users\Tony\Desktop\rails\deopt>rails server
8. 打开浏览器,看看效果:
http://localhost:3000/products
点击New Product
9. 添加缺失字段:(price)
C:\Users\Tony\Desktop\rails>rails generate migration add_price_to_product price:decimal
编辑文件:
C:\Users\Tony\Desktop\rails\deopt\db\migrate\20120115034639_add_price_to_product.rb
编辑后的代码为:
class AddPriceToProduct < ActiveRecord::Migration
def self.up
add_column :products, :price, :decimal,
:precision => 8, :scale => 2, :default => 0
end
def self.down
remove_column :products, :price
end
end
10. 再次运行数据迁移
C:\Users\Tony\Desktop\rails\deopt>rake db:migrate
11. 上步执行后,我们仅仅处理了模型,控制的流程并没有改变,也就是说控制器不需要改变,需要改变的是视图,我们还需要修改四个文件:
11.1 C:\Users\Tony\Desktop\rails\deopt\app\views\products\index.html.erb
<h1>Listing products</h1>
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>Image url</th>
<th>Price</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @products.each do |product| %>
<tr>
<td><%=h product.title %></td>
<td><%=h product.description %></td>
<td><%=h product.image_url %></td>
<td><%=h product.price %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Product', new_product_path %>
11.2 C:\Users\Tony\Desktop\rails\deopt\app\views\products\new.html.erb
<h1>New product</h1>
<% form_for(@product) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br/>
<%= f.text_field :title %>
</P>
<p>
<%= f.label :description %><br />
<%= f.text_area :description, :rows => 6 %>
</p>
<p>
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</P>
<p>
<%= f.label :Price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<!--<%= render 'form' %>-->
<% end %>
<%= link_to 'Back', products_path %>
11.3 C:\Users\Tony\Desktop\rails\deopt\app\views\products\edit.html.erb
<h1>Editing product</h1>
<% form_for(@product) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p>
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<% end %>
<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>
11.4 C:\Users\Tony\Desktop\rails\deopt\app\views\products\show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Title:</b>
<%=h @product.title %>
</p>
<p>
<b>Description:</b>
<%= @product.description %>
</p>
<p>
<b>Image url:</b>
<%=h @product.image_url %>
</p>
<p>
<b>Price:</b>
<%=h @product.price %>
</p>
<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
12. 点击New Product后页面最上方显示了这样的错误:error_messages_for was removed from Rails and is now available as a plugin
需要做的一些修改:
12.1 在 C:\Users\Tony\Desktop\rails\deopt\script下执行下面命令:
C:\Users\Tony\Desktop\rails\deopt\script>rails plugin install git://github.com/rails/dynamic_form.git
12.2 修改文件:C:\Users\Tony\Desktop\rails\deopt\Gemfile
添加一行: gem 'dynamic_form'
修改后的代码:
source 'http://rubygems.org'
gem 'rails', '3.0.9'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
gem 'dynamic_form'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
# gem 'ruby-debug'
# gem 'ruby-debug19', :require => 'ruby-debug'
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
# group :development, :test do
# gem 'webrat'
# end
12.3 执行
C:\Users\Tony\Desktop\rails\deopt>bundle install
12.4 可以使用:
C:\Users\Tony\Desktop\rails\deopt>bundle show
查看所有gems的bundle。
下一篇: php实现统计网站在线人数的方法_PHP