2013-05-17 28 views
0

我想從另一個表中添加一個字段。這些表格已經與連接表和模型相匹配。 視圖索引可以是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 
+0

使用我不知道你想做什麼。你可以添加你已經嘗試過的東西/你當前的表單和新的/編輯動作的代碼,以及你希望最終格式化數據的方式嗎? –

回答

0

如果我理解你,你正試圖以單一形式處理多個模型。看到這個railscast(http://railscasts.com/episodes/196-nested-model-form-part-1)。它會幫助你。如果沒有,請提供你的模型和表格的代碼。

+0

嗨,感謝這個鏈接,我是編程新手。如果不使用漂亮的發電機,這會有所作爲。 – user2333742

+0

嗨,漂亮的發電機只是爲了舒適的腳手架代。如果你真的想使用它們,你可以使用它們。 「accep_nested_attributes_for」是rails的特徵,而不是'漂亮的生成器'。 –

+0

您還可以查看'nested_form'gem和'cocoon'gem,以便構建漂亮的嵌套表單。 –

0

嵌套的屬性是正確的做法在這裏我給他們解釋我對Rails 5 here

+0

請從您的博客文章引用。還附加代碼來解釋你的觀點。不要只在你的答案中粘貼鏈接。 –

相關問題