【问题标题】:Importing data from a form?从表单导入数据?
【发布时间】:2012-07-30 23:37:54
【问题描述】:

我正在尝试导入四个数据字段。 Child_idFirst_nameLast_nameMedical。在我的表格中,它只是拉入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.firstchild.last。但是我想与child_id 之类的复选框一起选择的那些字段是。

现在我的表中保存了一个 id 为 8 的孩子,它将拉入 8,但 child.firstchild.last 的字段并没有拉入 id 所在的新表中。

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    嗯,“导入数据字段”是指在表单中显示 child 的属性? 我觉得这个表格没问题,现在它取决于这个表格之外的东西。

    我会检查以下内容:

    • child 的字段是否确实命名为 firstlastphoto 代码 sn-p,与您在问题中列出的代码相反?
    • @account@account.children的内容是什么?您可以将两者都输出到您的页面上进行检查。

    【讨论】:

    • 如果您的意思是“将数据从浏览器/表单发送到服务器/rails”并导入 - 您只有一个 child_id 字段,没有其他字段,所以这就是原因,但我认为这是故意的。
    • 我刚刚更新了我的问题。我正在查看的内容的更多细节。我认为你的答案便当就是我在正确的方向上寻找的东西。谢谢
    • 对不起,现在我更困惑你的问题是什么:a)如何在浏览器的页面上显示子信息 b)如何将用户输入从表单传输到 rails(并保存它在数据库中)?
    【解决方案2】:

    我在您的表单块中只看到一个表单标签:f.check_box :child_id<%= child.first %> 之类的其他内容不是表单的一部分,即使它们在表单块中。

    编辑:

    有很多问题。首先,严格按照关联建立的方式,CheckIn 不应该有 child_id 属性。它 has_many :children 而 Child belongs_to :check_in。 CheckIn 不应该有 child_id,Child 应该有 check_in_id。因此,您应该使用 check_in_id 值更新每个选定的孩子。我会阅读 ActiveRecord 关联:http://guides.rubyonrails.org/association_basics.html

    其次,表单呈现控件的方式我认为您最终会得到多个具有相同名称的复选框。当 rails 组装 params 散列时,它将忽略除最后一个具有特定键的散列项之外的所有项。因此,即使其他所有设置都正确,您仍然只能保存一个孩子进行签到。我会观看有关嵌套属性的本教程: http://railscasts.com/episodes/196-nested-model-form-part-1?view=asciicast

    最后,当你说它不保存 child.first 和 child.last(又名 first_name 和 last_name?)时,我不明白你的意思。该信息已经存储在子对象中,对吗?为什么要保存在其他地方?

    如果所有这些都正常工作,您将能够执行以下操作:

    # find an account
    account = Account.find(99)
    
    # create a check_in
    check_in = CheckIn.create
    
    # save one or more (or all) of account's children to the check_in
    check_in.children << account.children
    
    # see the first name of a child associated with a check_in
    check_in.children[0].first
    

    【讨论】:

    • 另外,如果一个 CheckIn 可以有很多孩子,你应该查看嵌套属性。我不确定它是否会按照您设置的方式工作。
    • 在你的困惑中,它给我带来了一点洞察力。我现在意识到你的意思是输入的信息不是表格的一部分。所以现在我明白我的问题是如何在选中复选框时将 child_first 和 child_last 作为表单的一部分与 :child_id 一起传入。这有帮助吗?
    • 我不清楚提交表单时更新的内容是什么。您可以将您的模型关联添加到问题中吗?描述控制器和操作也会有所帮助。
    • 我的关联工作正常。我可以拉入child_id。正确配置关联后,我可以在可能的事情列表中做前两件事。我可以将子表中的 id 提取到我的 check_in 表中。只是孩子的名字和姓氏不会进入 check_in 表。还有其他想法吗?
    • 那我一定是错过了什么。您可以使用 hidden_​​field 标签作为第一个和最后一个。
    猜你喜欢
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多