要从另一个连接表添加的字段

时间:2013-05-17 23:44:20

标签: ruby-on-rails

我想从另一个表中添加一个字段。这些表已与连接表和模型匹配。 视图索引可以是dipayed,它可以很好地工作。

<%= @something.map(&:name).join(", ") %>

有人可以建议如何将字段添加到"new"/"edit" "form"的内容吗?

需要文本字段或“用户”的完整详细信息,例如姓名,地址等。非常感谢代码输入的建议。

使用的代码:model:user,appointment,view / _form_for appointment。我正在尝试将用户和患者添加到约会表中,但它看起来相当复杂。

attr_accessible :email, :password, :password_confirmation,
  :remember_me, :dob, :first_name, :last_name, :gender, :doctor,
  :pps_no, :specialisation_id, :roles, :roles_id, :roles_name,
  :appointments, :appointments_id

# attr_accessible :title, :body
has_many :appointments, :through => :users_appointments
has_many :users_appointments
has_and_belongs_to_many :specialisations
has_many :roles, :through => :roles_users
has_many :roles_users


<%= form_for(@appointment) do |f| %>   
  <% if @appointment.errors.any?> %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@appointment.errors.count, "error") %> prohibited this appointment from being saved:
      </h2>
      <ul>
        <% @appointment.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>   
  <% end %>

  <div class="field">
    <%= f.label :date_time %><br />
    <%= f.datetime_select :date_time %>   
  </div>   
  <div class="field">
    <%= f.label :price %><br />
    <%= f.text_field :price %>   
  </div>   
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description %>   
  </div>

  <div class="users">
    <%= f.label :users %>
    <% @users.each do |user| %>
        <p>
        <%= check_box_tag "appointment[user_ids][]", user.id, @appointment.user.include?(user) %>
        <%= user.email %> </p>  
    <% end %>  
  </div>
  <div class="actions"> <%= f.submit %>  </div>
<% end %>



class Appointment < ActiveRecord::Base  
  attr_accessible :date_time, :description, :price, :users, :user_id, :users_ids 
  has_many :users, :through => :users_appointments   
  has_many :users_appointments 
end

2 个答案:

答案 0 :(得分:0)

如果我理解你,你试图以单一形式处理多个模型。请参阅此railscast(http://railscasts.com/episodes/196-nested-model-form-part-1)。它会对你有所帮助。如果没有,请提供您的型号和表格代码。

答案 1 :(得分:0)

嵌套属性是正确的方法在这里我解释它们,因为我使用Rails 5 here