我使用Cocoon gem构建一个嵌套表单,其中field_for包含另一个field_for。层次结构如下所示: - 信件形式 -Card field_for -Button field_for
我有一个link_to_add_association
,以便动态添加卡片及其按钮。这部分看起来像这样:
<div class="connected-carousels">
<div class="stage">
<div class="carousel carousel-stage">
<ul id="carousel-stage-ul">
<%= f.fields_for :cards do |card_fields| %>
<% if @blabla %>
<%= render 'card_fields', f: card_fields %>
<% end %>
<% end %>
<% end %>
</ul>
</div>
<a href="#" class="prev prev-stage"><span>‹</span></a>
<a href="#" class="next next-stage"><span>›</span></a>
</div>
<div class="navigation">
<a href="#" class="prev prev-navigation">‹</a>
<a href="#" class="next next-navigation">›</a>
<div class="carousel carousel-navigation">
<ul id="carousel-navigation-ul"></ul>
</div>
</div>
<div>
<%= link_to_add_association '+ Add Card', f, :cards, id: 'add-card-button-bis', data: { association_insertion_node: '#carousel-stage-ul', association_insertion_method: :append } %>
</div>
</div>
_card_fields.html.erb部分呈现按钮:
<% f.object.buttons.build %>
<%= f.fields_for :buttons do |button_card_fields| %>
<%= render 'button_fields', f: button_card_fields %>
<% end %>
_button_fields.html.erb partial:
<div class="add-button-card-modal">
<h4>Add New Button</h4>
<label>Button Text</label>
<%= f.text_field :button_text, :maxlength => 20, placeholder: "Enter the text to display on the button..." %>
<br><br>
<label>Button URL</label>
<%= f.text_field :button_url, placeholder: "Paste URL..." %>
<div class="nav-popups-buttons">
<button type="button" id="validate_new_card_button" class="small-cta2">Add Button</button>
<p class="remove-link" id="delete_new_card_button">Remove Button</p>
</div>
</div>
信函模特:
class Letter < ApplicationRecord
validates :campaign_name, :presence => true
belongs_to :core_bot
has_many :messages, dependent: :destroy
has_many :cards, dependent: :destroy
has_many :filters, dependent: :destroy
has_many :analytic_deliveries, dependent: :destroy
has_many :analytic_reads, dependent: :destroy
has_many :analytic_sends, dependent: :destroy
accepts_nested_attributes_for :filters, allow_destroy: true, :reject_if => :all_blank
accepts_nested_attributes_for :messages, allow_destroy: true, :reject_if => :all_blank
accepts_nested_attributes_for :cards, allow_destroy: true, :reject_if => :all_blank
end
卡片型号:
class Card < ApplicationRecord
validates :remote_image_url, :format => URI::regexp(%w(http https)), presence: { message: '%{value} : please enter valid url' }, :allow_blank => true
validates :title, :subtitle, :presence => true
belongs_to :letter, optional: true
has_many :buttons, dependent: :destroy
accepts_nested_attributes_for :buttons, :reject_if => Proc.new { |att| att[:button_text].blank? && att[:button_url].blank? }, allow_destroy: true
end
按钮型号:
class Button < ApplicationRecord
validates :button_url, :format => URI::regexp(%w(http https)), presence: { message: '%{value} : please enter valid url' },
unless: Proc.new { |a| a.button_url.blank? }
validates :button_text, :presence => true, unless: Proc.new { |a| a.button_url.blank? }
belongs_to :message, optional: true
belongs_to :card, optional: true
has_one :short_url, dependent: :destroy
end
如果出现错误,请在字母控制器中创建操作:
@letter.filters.build
if params[:letter]['cards_attributes'].present? == false
@letter.cards.build.buttons.build
end
format.html { render :new }
format.json { render json: @letter.errors, status: :unprocessable_entity }
问题是当创建操作引发错误或编辑记录时,不会构建按钮。如果我将@letter.cards.build.buttons.build
添加到控制器中,它会添加一张新卡但按钮输入仍然无法显示。
UPDATE 事实上按钮是建立的。唯一的问题是我使用自定义jquery来显示弹出窗口,显示按钮字段不起作用,因为在cocoon中创建了:after-insert回调。
当出现错误或编辑视图时,我创建的卡片将被渲染,而不是由link_to_add_association添加。这就是cocoon:没有调用after-insert回调......
任何想法如何使用link_to_add_association的相同逻辑调用渲染卡片字段上的插入后回调?
答案 0 :(得分:1)
好的,通常这些问题中存在一些问题。这个答案只是为了解决为什么Edit
视图总是无法加载的问题。
我最初的猜测是在控制器中,有两个问题...... if params
&amp; @letter.cards.build.buttons.build
是问题&amp;而不是试图围绕它设计 - 你需要改变它,接受任何错误和&amp;然后在模型中修复该错误。第一个修复是从那里获取条件if params
。
我也注意到你的信件模型上没有accept_nested_attributes_for :buttons
。
最后,在我们开始解决问题之前 - 我忘了询问你的strong_params部分 - 请发布create
&amp;的整个控制器操作控制器私有部分中的strong_params方法。
因为它根本没有加载。永远。这可能不是一个完全jquery问题。我们应该看到轨道生成所有卡片的集合&amp;通过该模型归属的按钮。
我猜是.build
方法是第一个问题...
我想如果你检查一下rails控制台...... rails c
......
首先,尝试调用@test = Letter.first
,然后在设置时探索它的值...您应该能够看到其中的其他ID&amp;然后可以用@test.{whatever}
来调用它们,即:@test.cards
,它将通过模型中的关系给出所有存在的卡片的集合。
其次,如果第一个失败,@test.cards.create!(name: "test")
以查看关系是否正确创建了条目。如果在控制台的@letter.cards.buttons
中显示任何内容,我会很高兴,然后我们会在稍后拍摄您的层次结构中的其他分支(letter.filters
)。
第三,我们通过控制台移动生成测试数据,以确保存在通过轨道控制器和放大器输入的值。到要显示的视图。
第四,我们检查jquery是否干扰它们的显示。
演示代码
这是3深嵌套...
注意所有这些工作&amp;对EDIT
不费力,_form.html.erb
中的new / create设置只是自动编辑。
模型
country.rb
class Country < ApplicationRecord
has_many :states
has_many :counties, through: :states
accepts_nested_attributes_for :states, reject_if: proc { |attributes| attributes[:name].blank? }, allow_destroy: true
accepts_nested_attributes_for :counties, reject_if: proc { |attributes| attributes[:name].blank? }, allow_destroy: true
end
state.rb
class State < ApplicationRecord
belongs_to :country
has_many :counties
validates :name, presence: true
# can't recall if this is needed
accepts_nested_attributes_for :counties, reject_if: proc { |attributes| attributes[:name].blank? }, allow_destroy: true
end
county.rb
class County < ApplicationRecord
belongs_to :state
validates :name
end
countries_controller.rb ...控制器(您甚至不需要为其他人生成控制器)
class CountriesController < ApplicationController
before_action :set_country, only: [:show, :edit, :update, :destroy]
def index
@countries = Country.paginate(page: params[:page], per_page: 10)
end
def show
end
def new
@country = Country.new
end
def edit
# @partial_choice = params[:partial_choice]
end
def create
@country = Country.new(country_params)
respond_to do |format|
if @country.save
format.html { redirect_to @country, notice: 'Country was successfully created.' }
format.json { render :show, status: :created, location: @country }
else
format.html { render :new }
format.json { render json: @country.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @country.update!(country_params)
format.html { redirect_to @country, notice: 'Country was successfully updated.' }
format.json { render :show, status: :ok, location: @country }
else
format.html { render :edit }
format.json { render json: @country.errors, status: :unprocessable_entity }
end
end
end
def destroy
@country.destroy
respond_to do |format|
format.html { redirect_to countries_url, notice: 'Country was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_country
@country = Country.find(params[:id])
end
def country_params
params.require(:country).permit(:id, :name, :description, :size, :player_id,
countryneighbor_attritubtes: [:id, :bordercountry_id, :country_id, :_destroy],
states_attributes: [:id, :name, :description, :country_id, :_destroy,
counties_attributes: [:id, :name, :description, :state_id, :_destroy]])
end
end
观点 - 如果您已经测试了数据和rails {1}}中的关系,strong_params
以及log/development.log
中已检查的服务器日志,然后可以比较这里视图中的HAML样式调用...
/basicB/app/views/countries/_form.html.haml
.fieldset.form-inline
= simple_form_for @country do |f|
= f.error_notification
.countries
= f.input :name, input_html: {:placeholder => "...insert country name..."}
= f.input :description
.row-fuild
%a.btn.btn-primary{"aria-controls" => "neighborsDisplay", "aria-expanded" => "false", "data-toggle" => "collapse", :href => "#neighborsDisplay"} Show Neighbors
#neighborsDisplay.collapse
.list-group
- for neighbor in @country.neighbors
= render partial: 'country_neighbors', locals: {neighbor: neighbor}
.states
= f.simple_fields_for :states do |state|
= render 'state_fields', :f => state
.links.row
= link_to_add_association 'Add State', f, :states, render_options: { wrapper: 'inline_form' }, :class => "btn btn-default"
.form-actions.row
= f.button :submit
/app/views/countries/_state_fields.html.haml
.nested-fields.list-group-item
.well
%h4 State
.form-inline.text
= f.input :name, input_html: {:placeholder => "...insert State Name ..."}
= f.input :description
= link_to_remove_association f, class: 'btn btn-default btn-xs' do
.glyphicon.glyphicon-remove
.counties
= f.simple_fields_for :counties do |state|
= render 'county_fields', :f => state
.links.row
= link_to_add_association 'Add County', f, :counties, render_options: { wrapper: 'inline_form' }, :class => "btn btn-default"
/app/views/countries/_county_fields.html.haml
.nested-fields.list-group-item.my-well
.row.text
= f.input :name, input_html: {:placeholder => "... County Name ..."}
= f.input :description
= link_to_remove_association f, class: 'btn btn-default btn-xs' do
.glyphicon.glyphicon-remove