Rails的RJS模板十一
迄今为止我们已完成"Thought Log"应用程序,并看到了RJS是如何整合到Rails内的。现在的时间是检验一个例子,该例子会更现实并解决一些你可能在你自己的实际工程内可能遇到的问题。
So far we have completed the "Thought Log" application and taken a look at how RJS fits into the Rails framework. Now it is time to examine an example that is a bit more realistic and solves some of the problems that you might actually run into in your own projects. My expenses have been getting out of hand lately, so let's build a simple application to help track them.
第一节 Creating the Models – 创建模型
首先,我们将运行 Rails 的模型生成器来创建贯通这个工程的模型。Rails模型生成器自动为模型及数据库迁移创建哑文件。然后我们将编辑被生成文件来添加我们自己的功能。
First, we'll run the Rails model generator to create the models used throughout this project. The Rails model generator automatically creates stub files for the models and database migrations. Then we'll edit the generated files to add our own functionality.
expenses> ruby script/generate model Project
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/project.rb
create test/unit/project_test.rb
create test/fixtures/projects.yml
create db/migrate
create db/migrate/001_create_projects.rb
expenses> ruby script/generate model Expense
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/expense.rb
create test/unit/expense_test.rb
create test/fixtures/expenses.yml
exists db/migrate
create db/migrate/002_create_expenses.rb
生成器在 app/models/project.rb 内创建 Project,在app/models/expense.rb 内创建 Expense,还有单元测试哑文件及测试用的 fixture。生成器也为我们创建了两个迁移文件:db/migrate/001_create_projects.rb与db/migrate/002_create_expenses.rb。现在模型生成器已经创建了这两个新迁移,我们需要添加将要由模型做为属性使用的列定义。现在我们将只跟踪每个工程的名字。打开 db/migrate/001_create_projects.rb 并像下面这样编辑它:
The generator creates the Project model in app/models/project.rb and the Expense model in app/models/expense.rb, along with unit test stubs and test fixtures. The generator also created two migrations for us: db/migrate/001_create_projects.rb and db/migrate/002_create_expenses.rb.
Now that the model generator has created these two new migrations, we need to add the column definitions that will be used by the models as attributes. For now we'll only track the name of each project. Open up db/migrate/001_create_projects.rb and edit it to look like this:
class CreateProjects < ActiveRecord::Migration
def self.up
create_table :projects do |t|
t.column :name, :string
end
end
def self.down
drop_table :projects
end
end
我们只添加单个行,t.column :name, :string,给迁移。该行添加列 name 的类型为string给数据库表projects。接着为expenses 表定义列。同样,打开db/migrate/002_create_expenses.rb并添加列project_id,description及amount。
We only added a single line, t.column :name, :string, to the migration. This line adds the column name of type String to the database table projects. Next, define the columns for the expenses table. Same routine: open up db/migrate/002_create_expenses.rb and add the columns project_id, description and amount.
class CreateExpenses < ActiveRecord::Migration
def self.up
create_table :expenses do |t|
t.column :project_id, :integer
t.column :description, :string
t.column :amount, :float
end
end
def self.down
drop_table :expenses
end
end
假设数据库已经创建完并且数据库连接也已配置好了,我们可以运行迁移。这会添加两个迁移文件内的列定义与表到在config/database.yml内配置的开发者数据库内。
Assuming that the database has already been created and the database connection has been configured, we can run the migrations. This will add the tables and columns defined in the two migration files to the development database configured in config/database.yml.
expenses> rake migrate
现在数据库包含了用于 Expense Tracker 的 schema ,我们可定义两个模型之间的关联了。一个Project 有多个Expense对象,因此添加has_many()关联给app/models/project.rb文件内的Project模型。
Now that the database contains the schema for the Expense Tracker we can define the relationships between the models. A Project has many Expense objects, so add the has_many() relationship to the Project model in the file app/models/project.rb.
class Project < ActiveRecord::Base
has_many :expenses, :dependent => :delete_all
end
我们添加 :dependent => :delete_all 选项给 has_many() 调用,因为我们不希望任何没有一个Project的孤儿expense留在数据库内。现在在Expense模型内定义belongs_to()关联。一个Expense对象belongs_to()一个Project,是因为Expense包含了外键。打开app/models/expense.rb文件。
We added the :dependent => :delete_all option to the has_many() call because we don't want any orphaned expenses lingering around in our database without a Project. Now define the belongs_to() relationship in the Expense model. An Expense object belongs_to() a Project because the Expense contains the foreign key. Open up app/models/expense.rb.
class Expense < ActiveRecord::Base
belongs_to :project
end
现在模型已定义完了并且数据库也存在了,我们可以继续下一步生成并定义控制器。
Now that the models are defined and the database is ready to go we can move on to the next step generating and defining the controllers.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步