让关联的model以下拉菜单(select)的形式显示
2012-02-14 20:15
197 查看
1. 先使用rails命令行generate scaffold来生成将要用到的各个组件。
a) Sex
rails generate scaffold Sex name:string
b) Person
rails generate scaffold Person name:string birthday:date salary:decimal sex:references
2. 生成数据库表:
rails db:migrate
3. 修改页面: 主要是在edit页面上让sex以下拉菜单的形式显示。
a) app/views/people/_form.html.erb
a) Sex
rails generate scaffold Sex name:string
b) Person
rails generate scaffold Person name:string birthday:date salary:decimal sex:references
2. 生成数据库表:
rails db:migrate
3. 修改页面: 主要是在edit页面上让sex以下拉菜单的形式显示。
a) app/views/people/_form.html.erb
<%= form_for(@person) do |f| %> <% if @person.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@person.errors.count, "error") %> prohibited this person from being saved:</h2> <ul> <% @person.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :birthday %><br /> <%= f.date_select :birthday %> </div> <div class="field"> <%= f.label :salary %><br /> <%= f.text_field :salary %> </div> <div class="field"> <%= f.label :sex %><br /> <%= f.select(:sex_id, Sex.all.map { |sex| [sex.name, sex.id] }, :prompt => true) %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>b) app/views/people/index.html.erb
<h1>Listing people</h1> <table> <tr> <th>Name</th> <th>Birthday</th> <th>Salary</th> <th>Sex</th> <th></th> <th></th> <th></th> </tr> <% @people.each do |person| %> <tr> <td><%= person.name %></td> <td><%= person.birthday %></td> <td><%= person.salary %></td> <td><%= person.sex.name %></td> <td><%= link_to 'Show', person %></td> <td><%= link_to 'Edit', edit_person_path(person) %></td> <td><%= link_to 'Destroy', person, confirm: 'Are you sure?', method: :delete %></td> </tr> <% end %> </table> <br /> <%= link_to 'New Person', new_person_path %>c)app/views/people/show.html.erb
<p id="notice"><%= notice %></p> <p> <b>Name:</b> <%= @person.name %> </p> <p> <b>Birthday:</b> <%= @person.birthday %> </p> <p> <b>Salary:</b> <%= @person.salary %> </p> <p> <b>Sex:</b> <%= @person.sex.name %> </p> <%= link_to 'Edit', edit_person_path(@person) %> | <%= link_to 'Back', people_path %>4. 修改model : person.rb 如下:
class Person < ActiveRecord::Base belongs_to :sex validates :sex_id, :presence => true validates :name, :uniqueness => true end
相关文章推荐
- admin.ModelAdmin 后台管理关联对象,某个字段怎么显示值
- EF5+MVC4系列(7) 后台SelectListItem传值给前台显示Select下拉框;后台Action接收浏览器传值的4种方式; 后台Action向前台View视图传递数据的四种方式(ViewDate,TempDate,ViewBag,Model (实际是ViewDate.Model传值))
- 登陆框显示下拉菜单 关联到保存的用户信息
- asp select下拉菜单选择图标并实时显示
- Android Dialog和adapter关联,列表形式显示对话框
- asp select下拉菜单选择图标并实时显示
- select下拉菜单所有选项均罗列显示
- 整数划分问题并显示每一种划分形式
- Mathematica进阶 - 自定义表达式显示形式
- ThinkPHP5 select出来的结果是个对象?居然还可以以数组形式访问数据?
- 设置闹钟——不让下拉菜单中显示闹钟信息
- asp.net学习之SqlDataSource 1 select的四种参数赋予形式的解释
- matlab下制作下拉菜单形式
- html 下拉菜单select 获取数据库内容
- 导入Excel 文件 数据显示成树形式
- select下拉菜单option文字粗体的实现方法
- Django model反向关联名称
- EL显示List里嵌套map(Spring MVC3)返回的model
- js二级下拉菜单的关联
- select2插件不兼容ie7,ie7下样子显示错位问题