2012-07-27 68 views
0

我想導入四個數據字段。 Child_idFirst_nameLast_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控制器創建方法。

DEF創建 @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的字段不會拉入新的表中。

回答

0

嗯,通過「導入數據字段」你的意思是顯示你的表單中的孩子的屬性? 窗體對我來說看起來不錯,現在它取決於這個窗體以外的東西。

我會檢查以下幾點:

  • 是孩子確實命名firstlastphoto在 使用的代碼片段的領域,相對於那些你在你的問題中列出?
  • @account@account.children的內容是什麼?你可以在你的頁面上輸出這兩個文件來檢查。
+0

的情況下,你的意思是「從瀏覽器/表單服務器/軌道發送數據」與導入 - 你只需要一個用於child_id的字段,其他字段不用,所以這就是原因,但我認爲這是有意的。 – bento 2012-07-27 12:51:00

+0

我剛剛更新了我的問題。關於我在看什麼的更多細節。我認爲你的答案是我正在尋找正確的方向。感謝 – 2012-07-27 13:29:29

+0

抱歉,現在我更加困惑你的問題是: a)如何在瀏覽器頁面上顯示子女信息 b)如何將用戶輸入從表單轉移到rails(並將其保存到數據庫中) ? – bento 2012-07-27 23:44:16

0

我只在表單塊中看到一個表單標籤:f.check_box :child_id。其他的東西,如<%= child.first %>不是表格的一部分,即使它們在表格塊內。

編輯:

有一些問題。首先,嚴格按照聯繫設置的方式進行,CheckIn不應具有child_id屬性。它has_many:孩子,而孩子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 
+0

另外,如果一個CheckIn可以有很多孩子,你應該查看嵌套屬性。我不確定它會按照你設置的方式工作。 – 2012-07-27 14:14:37

+0

在你的困惑中,它給我帶來了一點洞察力。我現在意識到你所說的信息並不是表格的一部分。所以現在我明白我的問題是如何將child_first和child_last作爲可以在複選框被選中時與:child_id一起引入的表單的一部分傳入。這有幫助嗎? – 2012-07-30 12:40:53

+0

我不清楚在提交表單時它的更新是什麼。你可以將模型關聯添加到問題中嗎?描述控制器和操作也會有所幫助。 – 2012-07-30 14:31:38