茧 link_to_add_association 不工作
Cocoon link_to_add_association not working
看过视频教程,翻来覆去的搜索,没看懂。非常感谢帮助。
我的主要问题是 link_to_add_association 不工作,而且我不断收到缺少模板的错误。
"ActionView::MissingTemplate in Applications#new"
它说:
缺少部分 applications/_question_fields、application/_question_fields 与 {:locale=>[:en]、:formats=>[:html]、:variants=>[]、:handlers=>[:生的,:erb, :html, :builder, :ruby, :coffee, :jbuilder]}。搜索:
*“/Users/AndyAir/projects/friends-app/app/views”
* "/Users/AndyAir/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/devise-4.2.1/app/views"
型号
class Application < ApplicationRecord
belongs_to :user
has_many :questions
accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
validates :name, :description, presence: true
end
控制器
class ApplicationsController < ApplicationController
before_action :find_application, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
@applications = Application.all.order("created_at DESC").limit(3)
end
def new
@application = Application.new
@application.questions.build
end
def create
@application = Application.new(application_params)
if @application.save
redirect_to @application
else
render 'new'
end
end
def show
end
def edit
end
def update
if @application.update(application_params)
redirect_to @application
else
render 'edit'
end
end
def destroy
@application.destroy
redirect_to root_path
end
private
def find_application
@application = Application.find(params[:id])
end
def application_params
params.require(:application).permit(:name, :description, questions_attributes: [:id, :name, :_destroy])
end
def after_sign_out_path_for(resource_or_scope)
redirect_to root_path
end
end
views/applications/new
<%= form_for @application do |f| %>
<% if @application.errors.any? %>
<div id="errors">
<p><%= @application.errors.count %> Prevented this application from saving</p>
<ul>
<% @application.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :name, placeholder: "Name of Your Friend Test", class: "form-control" %><br>
<%= f.label :description %><br>
<%= f.text_field :description, placeholder: "Write a witty tagline for your test.", class: "form-control" %><br>
</div>
<h3>Questions</h3>
<%= f.fields_for :questions do |question| %>
<div id="questions">
<div class="field">
<%= render 'questions_fields', f: question %>
<div class="links">
<%= link_to_add_association 'Add Question', f, :questions %>
</div>
</div>
</div>
<% end %>
<br>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
views/applications/_questions_fields.html.erb
<div class="nested-fields">
<%= f.text_field :question %>
<%= link_to_remove_association "remove task", f %>
</div>
请尝试从 fields_for.As 中取出 link_to_add_association 还没有问题,link_to_add_association 永远不会显示。
<%= f.fields_for :questions do |question| %>
<div id="questions">
<div class="field">
<%= render 'questions_fields', f: question %>
</div>
</div>
<% end %>
<div class="links">
<%= link_to_add_association 'Add Question', f, :questions %>
</div>
您的视图命名不正确,以便 cocoon 自动推导。当您有 _questions_fields
时,它正在寻找 _question_fields
(单数)。
要么你重命名部分(它只包含一个问题,所以恕我直言,它看起来更合乎逻辑),但你也可以 overrule the default partial name 如下:
<%= link_to_add_association 'Add Question', f, :questions, partial: 'questions_fields' %>
看过视频教程,翻来覆去的搜索,没看懂。非常感谢帮助。
我的主要问题是 link_to_add_association 不工作,而且我不断收到缺少模板的错误。
"ActionView::MissingTemplate in Applications#new"
它说: 缺少部分 applications/_question_fields、application/_question_fields 与 {:locale=>[:en]、:formats=>[:html]、:variants=>[]、:handlers=>[:生的,:erb, :html, :builder, :ruby, :coffee, :jbuilder]}。搜索: *“/Users/AndyAir/projects/friends-app/app/views” * "/Users/AndyAir/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/devise-4.2.1/app/views"
型号
class Application < ApplicationRecord
belongs_to :user
has_many :questions
accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
validates :name, :description, presence: true
end
控制器
class ApplicationsController < ApplicationController
before_action :find_application, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
@applications = Application.all.order("created_at DESC").limit(3)
end
def new
@application = Application.new
@application.questions.build
end
def create
@application = Application.new(application_params)
if @application.save
redirect_to @application
else
render 'new'
end
end
def show
end
def edit
end
def update
if @application.update(application_params)
redirect_to @application
else
render 'edit'
end
end
def destroy
@application.destroy
redirect_to root_path
end
private
def find_application
@application = Application.find(params[:id])
end
def application_params
params.require(:application).permit(:name, :description, questions_attributes: [:id, :name, :_destroy])
end
def after_sign_out_path_for(resource_or_scope)
redirect_to root_path
end
end
views/applications/new
<%= form_for @application do |f| %>
<% if @application.errors.any? %>
<div id="errors">
<p><%= @application.errors.count %> Prevented this application from saving</p>
<ul>
<% @application.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :name, placeholder: "Name of Your Friend Test", class: "form-control" %><br>
<%= f.label :description %><br>
<%= f.text_field :description, placeholder: "Write a witty tagline for your test.", class: "form-control" %><br>
</div>
<h3>Questions</h3>
<%= f.fields_for :questions do |question| %>
<div id="questions">
<div class="field">
<%= render 'questions_fields', f: question %>
<div class="links">
<%= link_to_add_association 'Add Question', f, :questions %>
</div>
</div>
</div>
<% end %>
<br>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
views/applications/_questions_fields.html.erb
<div class="nested-fields">
<%= f.text_field :question %>
<%= link_to_remove_association "remove task", f %>
</div>
请尝试从 fields_for.As 中取出 link_to_add_association 还没有问题,link_to_add_association 永远不会显示。
<%= f.fields_for :questions do |question| %>
<div id="questions">
<div class="field">
<%= render 'questions_fields', f: question %>
</div>
</div>
<% end %>
<div class="links">
<%= link_to_add_association 'Add Question', f, :questions %>
</div>
您的视图命名不正确,以便 cocoon 自动推导。当您有 _questions_fields
时,它正在寻找 _question_fields
(单数)。
要么你重命名部分(它只包含一个问题,所以恕我直言,它看起来更合乎逻辑),但你也可以 overrule the default partial name 如下:
<%= link_to_add_association 'Add Question', f, :questions, partial: 'questions_fields' %>