首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >茧link_to_add_association不起作用

茧link_to_add_association不起作用
EN

Stack Overflow用户
提问于 2017-03-29 03:55:38
回答 2查看 5.5K关注 0票数 0

跟随视频教程,一遍又一遍地搜索,不明白。会很感激你的帮助。

我的主要问题是,link_to_add_association不工作,我一直得到一个缺少的模板错误。

“ActionView::Applications#new中的错模板”

它的标题是:缺少部分应用程序/_问号_字段、带{:locale=>:en、:formats=>:html、:variants=>[]、:handlers=>:raw、:erb、:html、:builder、:ruby、:coffee、:jbuilder}的application/_质询_field。搜索:* "/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”

模型

代码语言:javascript
复制
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

控制器

代码语言:javascript
复制
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

意见/应用/新的

代码语言:javascript
复制
<%= 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 %>

视图/应用程序/_问号_字段.html.erb

代码语言:javascript
复制
<div class="nested-fields">
  <%= f.text_field :question %>
  <%= link_to_remove_association "remove task", f %>
</div>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-30 08:30:28

你的观点被错误地命名为茧自动推断。它正在寻找_question_fields (单数),而您有_questions_fields

您可以重命名这个部分(它只包含一个问题,因此看起来更符合逻辑),但是您也可以按如下所示进行取消默认的部分名称。

代码语言:javascript
复制
<%= link_to_add_association 'Add Question', f, :questions, partial: 'questions_fields' %>
票数 2
EN

Stack Overflow用户

发布于 2017-03-29 05:26:56

请尝试将link_to_add_association从fields_for.As中删除--还没有任何问题,link_to_add_association从未显示过。

代码语言:javascript
复制
<%= 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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43084014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档