开始使用 Ruby on Rails 时遇到问题

发布于 2024-08-13 19:18:40 字数 1394 浏览 1 评论 0原文

我想知道是否有人可以解决我遇到的一些问题?我创建了一个rails应用程序:

rails myapp -d mysql
cd myapp
haml --rails .
rake db:create:all

然后我想使用mysql客户端来创建表。比如说用户和客户。客户也是用户,因此您有这样的架构:

users
----------------
id         int, not null, primary key, auto increment
first_name varchar(50) not null
last_name  varchar(50) not null
email      varchar(50) not null unique
password   varchar(50) not null
created_at datetime not null
updated_at datetime not null

customers
----------------
id         int, not null, primary key, auto increment
user_id    int, unique
-- some other stuff that is customer specific

我需要运行哪些rails脚本命令才能在我的rails应用程序下创建并完全填写模型、视图和控制器?我尝试了这个:

ruby script/generate scaffold user
ruby script/generate scaffold customer

它创建了文件,但模型是空的:

class User < ActiveRecord::Base
end

这是怎么回事?另外,我想创建一个管理部分来管理东西。我发现我需要为这些添加路由:

map.namespace :admin do |admin|
  admin.resources :users
  admin.resources :customers
end

我还需要什么才能让管理部分运行? 这里还有我正在运行的 ruby​​/gems 版本:

ruby 1.8.6
rails 2.3.5 & 2.3.2 <- I'm using 2.3.2 because haml
  wasn't working (or some other plugin) with 2.3.5
haml 2.2.15
rspec 1.2.9 <- I saw from another thread that I might need
  this when creating an adminstration section (rspec_controller etc)

I'm wondering if someone can address some of the issues I am having? I create a rails app:

rails myapp -d mysql
cd myapp
haml --rails .
rake db:create:all

Then I want to use a mysql client to create tables. Lets say users and customers. A customer is also a user so you have schema like this:

users
----------------
id         int, not null, primary key, auto increment
first_name varchar(50) not null
last_name  varchar(50) not null
email      varchar(50) not null unique
password   varchar(50) not null
created_at datetime not null
updated_at datetime not null

customers
----------------
id         int, not null, primary key, auto increment
user_id    int, unique
-- some other stuff that is customer specific

what rails script commands do I need to run to get model, views and controllers created and completely filled out under my rails app? I tried this:

ruby script/generate scaffold user
ruby script/generate scaffold customer

which creates the files but the models are empty:

class User < ActiveRecord::Base
end

whats the deal? Also, I want to create an administration section to manage stuff. I figured out that I need to add routes for those:

map.namespace :admin do |admin|
  admin.resources :users
  admin.resources :customers
end

what else do I need to get the administration section going?
Also here are the versions of ruby/gems I am running:

ruby 1.8.6
rails 2.3.5 & 2.3.2 <- I'm using 2.3.2 because haml
  wasn't working (or some other plugin) with 2.3.5
haml 2.2.15
rspec 1.2.9 <- I saw from another thread that I might need
  this when creating an adminstration section (rspec_controller etc)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷心人i 2024-08-20 19:18:40

默认情况下,模型应该为空,因为数据库架构保存到 schema.rb 文件中,并使用 迁移

从您的回答中,我了解到您正在寻找一个预打包解决方案来编写一些配置并获取从控制器到为您准备的管理的所有内容。
抱歉,Rails 不向您提供此功能。如果您想要一个管理部分,您实际上必须对其进行编码。

它包括:

  1. 创建您的视图和模板
  2. 创建您的操作
  3. 映射您的路线 编写
  4. 您的测试

脚手架只为您提供一个起点,但这是一个您应该根据您的需求进行调整和扩展的起点。

如果您希望脚手架根据您的数据库表自动生成初始视图,您可以将参数传递给命令行工具

ruby script/generate scaffold user name:string age:integer

,但如果您想稍后添加新字段,则必须编写新的迁移并编辑您相应的观点/行动。

更多信息请参阅 Rails 指南维基

Models are supposed to be empty by default because database schema is saved into the schema.rb file and managed using migrations.

From your answer I understand you are looking for a prepackage solution to write a couple of configurations and get everything, from controller to administration cooked for you.
I'm sorry, Rails doesn't offer you this feature. If you want an administration section you actually have to code it.

It includes:

  1. creating your views and templates
  2. creating your actions
  3. mapping your routes
  4. writing your tests

The scaffold only provides you a starting point but this is a starting point you should adapt and extend to your needs.

If you want the scaffold to auto-generate your initial views according to your database table, you can pass the arguments to the command line tool

ruby script/generate scaffold user name:string age:integer

But if you want to add a new field later, you'll have to write a new migration and edit your views/actions accordingly.

More information are available in the Rails Guides and Wiki.

も星光 2024-08-20 19:18:40

Rails 专为数据库独立性而设计,所有“创建”都是通过位于 db/migrate 中的迁移完成的。

要创建适当的数据库表,您只需运行 rake db:migrate 即可执行任何迁移以创建必要的数据库表。

Rails Guides 是获取更多信息的好地方,其中有一个示例应用程序可供使用。

Rails is designed for database independence with all the 'creation' done via the migrations located in db/migrate.

To create the appropriate DB tables you then simply run rake db:migrate and any migrations will be executed to create the necessary DB tables.

A good place for more information is the Rails Guides which has an example application to work through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文