2014-02-11 26 views
1

我想讓用戶在同一表單的兩個不同下拉菜單中輸入兩個不同的東西,它會將整數存儲到檢查表中。Rails模式中的多個輸入字段以一個整數屬性

我希望用戶能夠在一個下拉列表中選擇model_name,在另一個下拉列表中選擇manufacturer。結果將在表格中存儲一個bat_id整數。 (告訴你用戶選擇哪隻蝙蝠)

我已經看到有幾個關於日期&時間的問題,但它們直接將值存儲在模型中。我正在嘗試存儲一個整數 - bat_id,以便bat_id將直接將審閱模型鏈接到蝙蝠模型。

例子我發現接近:

我現在的形式:

<%= form_for(@review) do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 
    <div class="field" align= "center"> 
    <h3>Select Brand</h3> 
    <%= f.collection_select :manufacturer_id, Manufacturer.all, :id, :manufacturer, include_blank: true %> 
    <h3>Select Bat</h3> 
    <%= f.grouped_collection_select :bat_id, Manufacturer.all, :bats, :manufacturer, :id, :model_year_and_name, include_blank: true %> 
    <h3>What do you like about this bat?</h3> 
    <%= f.text_area :pros, placeholder: "Enter what you like..." %> 
    <h3>What do you not like about this bat?</h3> 
    <%= f.text_area :cons, placeholder: "Enter what you don't like..." %></br> 
    </div> 
    <div align="center"> 
    <%= f.submit "Add Review", class: "btn btn-large btn-info" %> 
    </div> 
<% end %> 

我正在提交到review表並試圖將這兩個都提交到bat_id屬性。

<h3>Select Brand</h3> 
<%= f.collection_select :manufacturer_id, Manufacturer.all, :id, :manufacturer, include_blank: true %> 
<h3>Select Bat</h3> 
<%= f.grouped_collection_select :bat_id, Manufacturer.all, :bats, :manufacturer, :id, :model_year_and_name, include_blank: true %> 

在我的蝙蝠模型我有:has_many :reviews &在我的評論模式我有:belongs_to :bat

UPDATE:是否可以使用隱藏字段的javascript和我的兩個輸入組合確定我的一個輸出bat_id?

更新我改變了我的下拉列表中的代碼是什麼在起作用,讓我在manufacturer_id & bat_id同時輸入時進行選擇。但我仍然認爲有一種方法可以在我的review模型中存儲一個值。我使用JavaScript非常相似,this

回答

0

從UI角度來看,這似乎違反的用戶將能夠對任何車型年&名稱與任何製造商相關聯,即使廠商不生產該產品年&名。

假設你會介紹一些JavaScript來處理這個問題,從rails的角度來看,你會得到兩個不明確的行爲:bat_id字段在同一個表單中。我認爲你需要這樣的:

<h3>Select Brand</h3> 
<%= f.collection_select :manufacturer_id, Manufacturer.all, :id, :manufacturer, include_blank: true %> 
<h3>Select Bat</h3> 
<%= f.collection_select :bat_id, Bat.all, :id, :model_year_and_name, include_blank: true %> 

或者你可以創建一個包含複合場一個下拉列表,像這樣:

<h3>Select Bat</h3> 
<%= f.collection_select :bat_id, Bat.all.sort {|a, b| a.manufacturer_model_year_and_name <=> b.manufacturer_model_year_and_name}, :id, :manufacturer_model_year_and_name, include_blank: true %> 

,然後在你的蝙蝠模型介紹是這樣的:

def manufacturer_model_year_and_name 
    "#{self.manufacturer.name}: #{self.model_year_and_name}" 
end 
+0

是的我打算使用JavaScript來顯示只有製造商被選中的製造商的蝙蝠。 - 這將消除破損的用戶界面問題是否正確?對於解決方案#1,您提供了冗餘,並通過存儲「manufacturer_id」兩次違反DRY原則。 (我已經將'manufacturer_id'存儲在蝙蝠模型中)我可以再做一次,但我的目標是使用DRY。對於解決方案#2它確實有效,但我會成爲100-200個蝙蝠,我不想在UI側以30個蝙蝠的形式重複製造商的名稱。 – Daniel

+0

我不確定這部分是什麼意思 - 你能解釋得更好嗎? 'Bat.all.sort {| a,b | a.manufacturer_model_year_and_name <=> b.manufacturer_model_year_and_name}'我的猜測是來自蝙蝠模型的所有字段都使用a.manufacturer ....和b.manufacturer ...在數組中排序,這些屬性是模型中定義的屬性。 '| a,b |'&'<=>'是什麼意思? – Daniel

+0

是的,如果您使用JavaScript來僅顯示選定製造商的蝙蝠,那麼解決了用戶界面問題。 –

0

正如您在其他答案中所述,您不需要在您的評論模型中存儲manufacturer_id

我會建議創建一個Manufacturer選擇不是訪問您的評論模型,但只是用於過濾表單上的蝙蝠列表。

做到這一點的最佳方法可能是將一些自定義數據屬性添加到Bat選擇。

<%= collection_select :manufacturer, :manufacturer_id, Manufacturer.all, :id, :manufacturer %> 
<%= f.select :bat_id, Bat.all.map{ |b| [b.model_year_and_name, b.id, {'data-manufacturer' => b.manufacturer_id}] } %> 

然後使用一些JavaScript來過濾Bat選擇當Manufacturer選擇改變。

不幸的是,您不能只將display: none設置爲選項元素來隱藏它。這並不隱藏許多瀏覽器的選項。所以最好的方法是在每次更改制造商選擇時使用一些jQuery來克隆原始選擇,並刪除與選定製造商無關的任何選項。像這樣:

// rename the original select and hide it 
$('#bat_id').attr('id', 'bat_id_original').hide(); 

$('#manufacturer_id').on('change', function() { 
    $('#bat_id').remove(); // remove any bat_id selects 
    $bat = $('#bat_id_original') 
     .clone() // clone the original 
     .attr('id', 'bat_id') // change the ID to the proper id 
     .insertAfter('#bat_id_original') // place it 
     .show() // show it 
     .find(':not(option[data-manufacturer="' + $(this).val() + '"])') 
      .remove(); // find all options by other manufacturers and remove them 
}); 

您可能需要改變一些事情讓這個在你的安裝工作,但你可以的jsfiddle這裏查看靜態演示:http://jsfiddle.net/JL6M5/

你可能需要拒絕manufacturer_id表單上提交的字段,avitevet已經指出了這個答案,應該有幫助:Rails: Ignoring non-existant attributes passed to create()

+0

我可以使用javascript或node.js拒絕'manufacturer_id'字段嗎?我寧願用一種黑客的語言來代替使用RoR。 – Daniel

+0

你試過看看它是否先工作?你甚至可能不需要拒絕'manufacturer_id';我沒有測試過提交帶有不存在屬性的表單時會發生什麼。它可能正常工作。否則,您可以使用javascript覆蓋表單提交併在將數據發送到服務器之前刪除該字段(您需要搜索使用javascript提交表單)。 –

+0

@ ravensfan55222好的,所以我決定在昨天建立的rails應用上測試一下,在這裏查看:http://secret-bayou-5954.herokuapp.com/reviews/new。你根本不需要拒絕'manufacturer_id',但是你必須建立一個稍微不同的方式(我已經編輯了我的文章以包含更新後的代碼,但是我也將整個代碼推到了我的回購站在https://github.com/levymetal/stackoverflow-bats,所以你可以在那裏查看工作代碼) –

相關問題