您不需要繁重的 Rails 管理框架来获得一对多的关系。
您可以使用脚手架来实现大部分目标。
在控制器和 _form 视图中做更多的工作会让你完成剩下的工作。
就是这样...
Story: Select a beer for a developer
In order to select a beer for a developer
As an admin
I want a mainly scaffolded interface to select that beer
Scenario 1: Select beer for new developer
Given I have clicked the <new> button and entered the developer's name
When I click the beer dropdown
Then I should be presented with a list of beers to choose from
And that beer will be saved when I click <ok>
Scenario 2: Select a different beer for existing developer
Given I have clicked the <edit> button on the index page for a particular developer
When I click the beer dropdown
Then I should be presented with a list of beers to choose from
And that beer will be saved when I click <ok>
假设我们有一张啤酒桌:
create_table "beers", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
还有一个开发者表,它有一个引用 beers 表的外键 (beer_id):
create_table "developers", force: true do |t|
t.string "name"
t.integer "beer_id"
t.datetime "created_at"
t.datetime "updated_at"
end
我们可以使用脚手架来创建两个表:
$ rails g 脚手架啤酒名称
$ rails g 脚手架开发人员名称 beer_id:integer
脚手架命令为每个模型创建控制器和视图。
我们需要稍微修改一下我们的控制器和视图,以便让下拉菜单为每个开发人员选择一种啤酒:
app/views/developers/_form.html.erb
将生成的 text_field 和 beer_id 标签替换为以下内容:
<div class="field">
<%= f.label :beer_id %><br />
<%= collection_select(:developer, :beer_id, @beers, :id, :name, {:prompt => false}) %>
</div>
应用程序/控制器/developer_controller.rb
编辑控制器的 new 和 edit 方法:
# GET /developers/new
def new
@developer = Developer.new
@beers = Beer.all
respond_to do |format|
format.html # new.html.erb
end
end
# GET /developers/1/edit
def edit
@beers = Beer.all
respond_to do |format|
format.html # new.html.erb
end
end
屏幕截图
笔记
Rails 脚手架很棒。查看它为您创建的所有文件:
$ be rails g scaffold beer name
invoke active_record
create db/migrate/20140912144218_create_beers.rb
create app/models/beer.rb
invoke rspec
create spec/models/beer_spec.rb
invoke factory_girl
create spec/factories/beers.rb
invoke resource_route
route resources :beers
invoke scaffold_controller
create app/controllers/beers_controller.rb
invoke erb
create app/views/beers
create app/views/beers/index.html.erb
create app/views/beers/edit.html.erb
create app/views/beers/show.html.erb
create app/views/beers/new.html.erb
create app/views/beers/_form.html.erb
invoke rspec
create spec/controllers/beers_controller_spec.rb
create spec/views/beers/edit.html.erb_spec.rb
create spec/views/beers/index.html.erb_spec.rb
create spec/views/beers/new.html.erb_spec.rb
create spec/views/beers/show.html.erb_spec.rb
create spec/routing/beers_routing_spec.rb
invoke rspec
create spec/requests/beers_spec.rb
invoke helper
create app/helpers/beers_helper.rb
invoke rspec
create spec/helpers/beers_helper_spec.rb
invoke jbuilder
create app/views/beers/index.json.jbuilder
create app/views/beers/show.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/beers.js.coffee
invoke scss
create app/assets/stylesheets/beers.css.scss
invoke scss
create app/assets/stylesheets/scaffolds.css.scss
您所要做的就是知道当您想要的不仅仅是基本的 CRUD 操作时要修改哪些文件。
希望有帮助。〜莱克斯