简体   繁体   English

以一种形式创建两个模型

[英]Creating two models in one form

This is a sample of my code:这是我的代码示例:

    <%= form_for [@facility, @owner],:html => {:multipart => true} do |f| %>
    <%= render 'shared/error_messages_doc' %>
    <table>
    <tr>
      <td valign=top> First Name </td>
      <td ><%= f.text_field :first_name %></td>
    </tr>
    <tr>
      <td valign=top> Last Name </td>
      <td ><%= f.text_field :last_name %></td>
    </tr>
    </table>
    <br>

    <table width = "750">
    <tr>
      <th>Day</th>
      <th>Start time</th>
      <th>End time</th>
    </tr>
    
    <%= f.fields_for :working_hours, @owner.working_hours do |wh| %>
    <div>
    <tr>
      <td><%= wh.object.week_day %></td>
      <td>
        <center><%= wh.text_field :start_time, :value => wh.object.start_time.strftime('%H:%M'), :style => "width: 100px;" %></center>
      </td>
      <td>
        <center><%= wh.text_field :end_time, :value => wh.object.end_time.strftime('%H:%M'), :style => "width: 100px;" %></center>
      </td>
    </tr>
    </div>
    <% end %>
  </table>
    <tr>
    <td><%= f.submit :class => "btn btn-primary" %></td>
    </tr>
    </table>
    <% end %>

As you can see I am trying to create a owner with many working_hours (7 working_hours, one for each weekday).正如您所看到的,我正在尝试创建一个拥有许多working_hours(7 个working_hours,每个工作日一个)的所有者。 The problem is that this is working with edit, but the fields for working_hours does not show up when I try to create a new owner.问题是这与编辑一起工作,但是当我尝试创建新所有者时,working_hours 的字段没有显示。 I think the problem is that @owner.working_hours is looking for working_hours from the owner, which does not exist at the moment.我认为问题在于@owner.working_hours 正在从所有者那里寻找working_hours,目前尚不存在。 Since I am creating a new owner, I need to create an array of 7 working_hours for the owner.由于我正在创建一个新所有者,因此我需要为所有者创建一个包含 7 个 working_hours 的数组。 How can I do this?我怎样才能做到这一点?

To create the working_hours use build .要创建working_hours使用build In your controller you might want something like:在您的控制器中,您可能需要以下内容:

class OwnersController < ApplicationController
  def new
    @owner = Owner.new
    @owner.working_hours.build(Time::DAYS_INTO_WEEK.map{|name, value| { day_of_week: value }})
  end
end

Probably a bit overkill using Time::DAYS_INTO_WEEK , but I don't know what your attributes of working_hours are;使用Time::DAYS_INTO_WEEK可能有点矫枉过正,但我​​不知道你的working_hours属性是什么; just an example of how you can also set a default value for the created objects.只是一个示例,说明您还可以如何为创建的对象设置默认值。

Just to explain, build can accept an array of hashes defining the attribute values of the newly created objects, so the above expands to:只是为了解释一下, build可以接受定义新创建对象的属性值的哈希数组,因此上述扩展为:

@owner.working_hours.build([{:day_of_week=>0}, {:day_of_week=>1}, {:day_of_week=>2}, {:day_of_week=>3}, {:day_of_week=>4}, {:day_of_week=>5}, {:day_of_week=>6}])

So as you can see, it's creating seven objects with the day_of_week attribute set.如您所见,它创建了七个设置了day_of_week属性的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM