让关联的model以下拉菜单(select)的形式显示
来源:互联网 发布:购物车保存到数据库 编辑:程序博客网 时间:2024/06/09 15:00
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
<%= 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 => trueend
- 让关联的model以下拉菜单(select)的形式显示
- 关于如何将action中传给jsp的值以下拉框的形式显示出来
- 使magento的分层菜单以下拉菜单显示插件 - Improved Layered Navigation
- UITableView的初始化(以下拉菜单式为例)
- 把图片文件夹中的子文件夹以下拉框的形式展示出来。并且选择不同的文件夹显示出各文件夹中的图片
- 让div以table的形式显示
- 用户名失去焦点ajax请求后台,页面以下拉列表的形式展示相应的数据
- C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值
- 关于html中下拉菜单select的样式的改变
- CSS简洁的左侧菜单(侧拉菜单,向右显示)
- magento------让菜单显示图片-------显示图片的菜单
- 让Chrome显示12px以下的字体
- 安卓三种形式的菜单
- 让superfish的菜单在左边显示
- 让隐藏的virtualBox菜单重新显示
- 在Excel中让数字以文本的形式显示
- 让数字以金额形式显示,去掉小数点的0
- 以指针的形式和以下标的形式访问指针?以指针的形式和以下标的形式访问数组?
- PG数据库的维护 postgresql
- 数据结构基础(3)-->队列
- Linux(ubuntu)下用eclipse对log4cplus库的安装,编译和使用-由张海伦指导完成
- awk 解决 sqlldr源数据换行问题
- SSH 整合报错处理记录---Unable to get the default Bean Validation factory
- 让关联的model以下拉菜单(select)的形式显示
- 谈谈注入工具的原理和开发
- Cf 105 Div.2
- 动态规划经典问题----0-1背包问题
- Ruby 和 Rails 的国际化与本地化技术
- 打印机 “错误-正在打印”
- 海盗湾就这么90M
- tcpserver: fatal: unable to bind: address already used
- 数据库建表注意事项