ruby-on-rails - 无法在 Rails 5.1 中保存嵌套表单

标签 ruby-on-rails ruby ruby-on-rails-5

我使用了nested_form gem,我正在尝试构建一个包含两个表(项目、问题)中的字段的表单。

我的模型:

class Project < ApplicationRecord
    has_many :questions
    accepts_nested_attributes_for :questions
end

class Question < ApplicationRecord
    belongs_to :project
end

我的 Controller :

class ProjectsController < ApplicationController
layout 'application'

def index
    @projects = Project.all
end

def show
    @project = Project.find(params[:id])
end

def new
    @project = Project.new
    @questions = @project.questions.build
end

def create
@project = Project.new(project_params)

@project.save
 respond_to do |format|
 if @project.save
    format.html { redirect_to @project, notice: 'Project was successfully   created.' }
    format.json { render :show, status: :created, location: @project }
 else
    format.html { render :new }
    format.json { render json: @project.errors, status: :unprocessable_entity   }
 end
 end
end

private

def project_params
    params.require(:project).permit(:name, question_attributes: [:id,   :content, :_delete])
end
end

我的看法:

<%= nested_form_for(@project) do |f| %>
 <% if @project.errors.any? %>
    <div id="error_explanation">
     <h2>
      <%= pluralize(@project.errors.count, "error") %> prohibited this     project from being saved:</h2>
      <ul>
       <% @project.errors.full_messages.each do |message| %>
       <li>
        <%= message %>
       </li>
       <% end %>
     </ul>
    </div>
   <% end %>
   <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
   </div>
   <%= f.fields_for :questions do |builder| %>
    <%= render "question_fields", :ff => builder %> 
   <% end %>
   <p>
    <%= f.link_to_add "Add a questions",:questions %>
   </p> 
   <div class="actions">
    <%= f.submit %>
   </div>
<% end %>

还有我的架构文件:

create_table "projects", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "questions", force: :cascade do |t|
t.integer "project_id"
t.string "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["project_id"], name: "index_questions_on_project_id", using: :btree
end

并且 _question_fields 文件是:

<p>
 <%= ff.label :content, "Question" %>
 <%= ff.text_area :content%>
 <%= ff.link_to_remove "Remove this task"%>
</p>

表项目将被保存,但表问题无法保存。为什么?

修改这行代码后

def project_params
  params.require(:project).permit(:name, questions_attributes: [:id,   :content, :_delete])
end

我收到以下错误:

1 个错误禁止保存此项目:

问题项目必须存在

我也应用了更改,但这次没有任何内容无法保存。

def project_params
  params.require(:project).permit(:name, questions_attributes: [:id,   :content, :project_id, :_delete])
end

最佳答案

问题出在 project_params 上。由于您有 has_many :questions,因此 question_attributes 应更改为 questions_attributes

def project_params
  params.require(:project).permit(:name, questions_attributes: [:id,   :content, :_delete])
end

更新:

Questions project must exist

允许 questions_attributes 中的 project_id

def project_params
  params.require(:project).permit(:name, questions_attributes: [:id,   :content, :project_id, :_delete])
end

或在关联上设置可选:true

class Question < ApplicationRecord
  belongs_to :project, optional: true
end

关于ruby-on-rails - 无法在 Rails 5.1 中保存嵌套表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44774912/

相关文章:

activerecord - Rails 5 枚举,其中 "like"

ruby-on-rails - Rails 5 中的 OR 子句

Mysql2::Error Access denied for user 'root' @'localhost' (using password: NO) ruby​​ on rails

ruby-on-rails - GZIPed 响应的 Rspec 测试

mysql - Ruby on Rails : Getting an AdapterNotSpecified for a website although it's been defined

ruby-on-rails - Ruby 是否缓存将评估为相同结果的方法的结果?

ruby - 如何匹配所有语言的字符,除了 ruby​​ 中的特殊字符

ruby-on-rails - 如何使用相同的Rpec测试来测试Rails项目的生产版本?

javascript - AngularJS - 是否可以将 {{expressions}} 存储为 $scope.variable 值?

ruby-on-rails-5 - Rails 5.2 带有 Cocoon 形式的 Active Storage