【发布时间】:2012-07-30 23:37:54
【问题描述】:
我正在尝试导入四个数据字段。 Child_id、First_name、Last_name、Medical。在我的表格中,它只是拉入child_id:
<%= form_for @check_in, :url => {:controller => 'check_ins', :action => 'create' } do |f| %>
<% @account.children.each do |child| %>
<%= f.check_box :child_id, {:checked => 'checked', :multiple => true}, child.id.to_s %>
<%= image_tag child.photo.url(:thumb) %>
<span class="name"><%= child.first %>
<%= child.last %></span><br/>
<% end %>
<% end %>
模型关联:
class CheckIn < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :account
belongs_to :check_in
end
这是我在 check_ins 控制器中的创建方法。
定义创建 @check_in = CheckIn.new(params[:check_in])
begin
params[:check_in] [:child_id].each do |child_id|
unless child_id == 0.to_s
CheckIn.new(:child_id => child_id).save!
end
end
respond_to do |format|
format.html { redirect_to(:controller => 'sessions', :action => 'new') }
format.json { render json: @check_in, status: :created, location: @check_in }
end
rescue
respond_to do |format|
format.html { render action: "new" }
format.json { render json: @check_in.errors, status: :unprocessable_entity }
end
end
结束
此表格也在展示页面上。复选框在那里,复选框旁边是从另一个表中提取的信息:child.first、child.last。但是我想与child_id 之类的复选框一起选择的那些字段是。
现在我的表中保存了一个 id 为 8 的孩子,它将拉入 8,但 child.first 和 child.last 的字段并没有拉入 id 所在的新表中。
【问题讨论】:
标签: ruby-on-rails ruby