當用戶在我的活動策劃應用程序中點擊AddDate時,我添加了一行,如下所示,因此用戶可以添加日期和開始/結束時間以及AM/PM。在rails中,如何將多個UI字段保存爲一個嵌套屬性?
但在我的表單中使用嵌套屬性,似乎我的選擇控件需要引用它們設置的模型字段(:start_time和:end_time)。但是開始和結束時間是由兩個選擇控件創建的,一個用於小時,另一個用於分鐘。所以我不確定在兩個選擇中選擇的值如何組合起來形成開始和結束時間。
<div class="user_event_inline_container margin_left_ten padding_right_gone">
<%= f.label :start_time, "Start", class: 'info_inline_control info_label five_margin_right' %>
<%= f.select :start_time, options_for_select([['1',1],['2',2],['3',3],['4',4],['5',5],['6',6],['7',7],['8',8],['9',9],['10',10],['11',11],['12',12]]), class: (field_class(@user_event, :start_time) + 'info_inline_control') %>
<%= f.select :start_time, options_for_select([['00',1],['15',2],['30',3],['45',4]]), class: (field_class(@user_event, :start_time) + 'info_inline_control') %>
<%= f.select :start_am_pm, options_for_select([['am',1],['pm',2]]), class: (field_class(@user_event, :start_am_pm) + 'info_inline_control') %>
</div>
<div class="user_event_inline_container margin_left_ten padding_right_gone">
<%= f.label :end_time, "End", class: 'info_inline_control info_label five_margin_right' %>
<%= f.select :end_time, options_for_select([['1',1],['2',2],['3',3],['4',4],['5',5],['6',6],['7',7],['8',8],['9',9],['10',10],['11',11],['12',12]]), class: (field_class(@user_event, :end_time) + 'info_inline_control') %>
<%= f.select :end_time, options_for_select([['00',1],['15',2],['30',3],['45',4]]), class: (field_class(@user_event, :end_time) + 'info_inline_control') %>
<%= f.select :end_am_pm, options_for_select([['am',1],['pm',2]]), class: (field_class(@user_event, :end_am_pm) + 'info_inline_control') %>
</div>
你能編輯你的問題,幷包含你的瀏覽器提交的表單的POST變量嗎?一種可能性是命名您的選擇字段f.select:start_hour f.select:start_minute,然後在您的控制器中連接這些字符串值並將它們分配給:start_time。目前我認爲每次添加一個名爲start_time的選擇時都會覆蓋start_time。 – ctilley79
你是對的,覆蓋屬性。我添加到EventDate模型 - attr_accessible:start_hour,:start_minute,:end_hour,:end_minute,但我得到這個錯誤 - 未定義的方法'start_hour'爲# –
也不知道要放在控制器。這是我的UserEvent控制器創建方法:def create @user_event = current_user.user_events.build(params [:user_event]) –