Rails-嵌套属性视图未显示

时间:2019-06-28 17:44:33

标签: ruby-on-rails ruby view nested-attributes

我正在使用联接表和嵌套属性在单独的表中收集投资者兴趣列表,以获取投资者资料。由于某些原因,我的视图为空,没有显示 investor_interest 文本字段以进行更新。您能找到原因吗?我的代码如下。

investor_profile.rb

class InvestorProfile < ApplicationRecord
  belongs_to :user

  has_many :investor_profile_interests
  has_many :investor_interests, through: :investor_profile_interests

  accepts_nested_attributes_for :investor_profile_interests

end

investor_interest.rb

class InvestorInterest < ApplicationRecord
  has_many :investor_profile_interests
  has_many :investor_profiles, through: :investor_profile_interests
end

investor_profile_interest.rb

class InvestorProfileInterest < ApplicationRecord
  belongs_to :investor_profile
  belongs_to :investor_interest

end

投资者概况控制者

def new
  @investor_profile = InvestorProfile.new
end

观看次数

<%= form_with model: @investor_profile, class: 'form new-profile-form' do |f| %>
   <div class="field">
      <%= f.fields_for :investor_profile_interests do |test| %>
        <%= test.text_field :investor_interest %>
      <% end %>
   </div>
  <div class="field">
      <%= f.submit 'Submit', class: 'button is-primary is-normal' %>
   </div>
<% end %>

1 个答案:

答案 0 :(得分:1)

在新方法中,建立Investor_profile_interests-

def new
  @investor_profile = InvestorProfile.new
  @investor_profile.investor_profile_interests.build
end