通过多选框实现多对多关系的数据输入
来源:百度文库 编辑:神马文学网 时间:2024/04/29 02:01:28
railscast第17集017_habtm_checkboxes的例解: rails demo -d mysqladmin -u root create demo_development ruby script/generate model productruby script/generate model category ---------001_create_products.rbclass CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.column :name, :string
t.column :price, :decimal
end
end def self.down
drop_table :products
end
end
--------002_create_categories.rbclass CreateCategories < ActiveRecord::Migration
def self.up
create_table :categories do |t|
t.column :name, :string
end
create_table :categories_products , :id=>false do |t|
t.column :product_id, :integer
t.column :category_id, :integer
end
end def self.down
drop_table :categories
end
end
运行rake db:migrate ruby script/generate product admin -f 在_form.rhtml中加入:
<%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %>
<%= category.name %>
<% end %>
在admin_controller.rb里的update这个方法里加入:def update
params[:product][:category_ids] ||= [] @product = Product.find(params[:id])
if @product.update_attributes(params[:product])
flash[:notice] = 'Product was successfully updated.'
redirect_to :action => 'show', :id => @product
else
render :action => 'edit'
end
end
def self.up
create_table :products do |t|
t.column :name, :string
t.column :price, :decimal
end
end def self.down
drop_table :products
end
end
--------002_create_categories.rbclass CreateCategories < ActiveRecord::Migration
def self.up
create_table :categories do |t|
t.column :name, :string
end
create_table :categories_products , :id=>false do |t|
t.column :product_id, :integer
t.column :category_id, :integer
end
end def self.down
drop_table :categories
end
end
运行rake db:migrate ruby script/generate product admin -f 在_form.rhtml中加入:
<% for category in Category.find(:all) %>
<%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %>
<%= category.name %>
<% end %>
在admin_controller.rb里的update这个方法里加入:def update
params[:product][:category_ids] ||= [] @product = Product.find(params[:id])
if @product.update_attributes(params[:product])
flash[:notice] = 'Product was successfully updated.'
redirect_to :action => 'show', :id => @product
else
render :action => 'edit'
end
end
通过多选框实现多对多关系的数据输入
通过xalan实现关系型数据库到XML的数据交换
通过VB结构数组输入数据VC编写DLL实现图形文件
Excel创建简单的数据输入窗体
使用JAVA通过ARP欺骗实现数据封包监听
Windows Forms中通过自定义组件实现统一的数据验证(一) - 一个程序员的自省 ...
通过对web日志的挖掘来实现内容推荐系统 - 数据结构和算法 - Tech - JavaE...
Oracle存储过程实现多线程对表数据的抽取 - 狮子尾巴的专栏 - CSDN博客
通过回收站误删的数据恢复
Excel中快速输入固定有规律的数据(图)
快速搜索并辅助输入所需的数据
Excel中快速输入固定有规律的数据(图)***
Excel中快速输入固定有规律的数据(图
与数据输入和计算有关的Excel快捷键
Excel中快速输入固定有规律的数据
Excel中输入各种类型数据的方法和
EXCEL中常用公式集合 ,excel 输入数据的方法
使用JAVA通过ARP欺骗实现数据封包监听 - Rexcj - BlogJava
提高Excel数据输入效率
Excel2007 数据快速输入技巧
多地公安机关将通过微博实现跨区域合作
如何通过B超单数据预知宝宝的性别
探讨通过VB向EXCEL传输数据的方法--
通过Cache机制实现通用的配置管理模块