Ruby on rails 要从另一个连接的表中添加的字段
我想从另一个表中添加一个字段。这些表已与联接表和模型匹配。 视图索引可以显示,并且工作得很好Ruby on rails 要从另一个连接的表中添加的字段,ruby-on-rails,Ruby On Rails,我想从另一个表中添加一个字段。这些表已与联接表和模型匹配。 视图索引可以显示,并且工作得很好 <%= @something.map(&:name).join(", ") %> 如果我理解你的意思,你正在试图用一种形式处理多个模型。请参见此railscast()。它会帮助你的。如果没有,请提供您的模型和表单的代码。嵌套属性是正确的方法,我在这里解释了它们,就像我在Rails 5中使用的一样。我不确定您想做什么。你能添加你尝试过的东西的代码/你当前的表单和新建/编辑操作,以及你
<%= @something.map(&:name).join(", ") %>
如果我理解你的意思,你正在试图用一种形式处理多个模型。请参见此railscast()。它会帮助你的。如果没有,请提供您的模型和表单的代码。嵌套属性是正确的方法,我在这里解释了它们,就像我在Rails 5中使用的一样。我不确定您想做什么。你能添加你尝试过的东西的代码/你当前的表单和新建/编辑操作,以及你希望数据最终如何格式化吗?嗨,谢谢这个链接,我是编程新手。如果不使用漂亮的发电机,那会有什么不同吗?嗨,漂亮的发电机只用于舒适的脚手架生成。如果你真的想用,你就用它们。“accepts_nested_attributes_for”是rails的功能,而不是“漂亮的生成器”。您还可以查看“nested_form”gem和“cocoon”gem以构建漂亮的嵌套表单。链接第196集是一个快速解决方案!谢谢!请引用你的博客文章。还可以添加代码来解释您的观点。不要只在答案中粘贴链接。
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