我在構建一個允許用戶創建比賽的應用程序。每場比賽都有很多問題,每場比賽都有很多條目。每個條目都有很多答案,每個問題都有很多答案。以下是我的型號:Rails嵌套窗體錯誤:param丟失或值爲空
class Answer < ActiveRecord::Base
belongs_to :entry
belongs_to :question
end
class Contest < ActiveRecord::Base
has_many :entries
has_many :questions
end
class Entry < ActiveRecord::Base
belongs_to :contest
has_many :answers
accepts_nested_attributes_for :answers, allow_destroy: true
end
class Question < ActiveRecord::Base
has_many :answers
belongs_to :contest
end
除了當我嘗試創建條目時,所有東西都可以使用。我得到一個「參數丟失或值是空的:輸入」錯誤。這裏是我的控制器:
class EntriesController < ApplicationController
before_action :set_entry, only: [:show, :edit, :update, :destroy]
before_action :set_contest
# GET /entries
# GET /entries.json
def index
@entries = Entry.all
end
# GET /entries/1
# GET /entries/1.json
def show
end
# GET /entries/new
def new
@entry = Entry.new
end
# GET /entries/1/edit
def edit
end
# POST /entries
# POST /entries.json
def create
@entry = Entry.new(entry_params)
@entry.contest = @contest
respond_to do |format|
if @entry.save
format.html { redirect_to @entry, notice: 'Entry was successfully created.' }
format.json { render :show, status: :created, location: @entry }
else
format.html { render :new }
format.json { render json: @entry.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /entries/1
# PATCH/PUT /entries/1.json
def update
respond_to do |format|
if @entry.update(entry_params)
format.html { redirect_to @entry, notice: 'Entry was successfully updated.' }
format.json { render :show, status: :ok, location: @entry }
else
format.html { render :edit }
format.json { render json: @entry.errors, status: :unprocessable_entity }
end
end
end
# DELETE /entries/1
# DELETE /entries/1.json
def destroy
@entry.destroy
respond_to do |format|
format.html { redirect_to entries_url, notice: 'Entry was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_entry
@entry = Entry.find(params[:id])
end
def set_contest
@contest = Contest.find(params[:contest_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def entry_params
params.require(:entry).permit(:contest_id, answers_attributes: [:id, :content, :entry_id, :question_id, :_destroy])
end
end
這裏是我的參賽表格:
<%= simple_form_for([@contest, @entry]) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<h3>Questions</h3>
<%= simple_fields_for :answers do |ff| %>
<% @contest.questions.each do |question| %>
<h4><%= question.content %></h4>
<%= ff.input :content, input_html: {class: 'form-control'} %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
我還在工作的邏輯,但很困惑,爲什麼報名表給我這個錯誤。任何幫助,將不勝感激!
UPDATE
在Rails的指南例如他們表現出新的行動:
def new
@person = Person.new
2.times { @person.addresses.build}
end
我需要建立在我的新行動的答案對象?我不確定...我嘗試過,但沒有奏效。我覺得這不可能是問題,但作爲錯誤從entry_params
方法
你在哪裏得到的錯誤?導軌控制檯應該給你一個行號,從中你應該能夠告訴我們錯誤來自哪裏。 – Jon 2015-02-24 06:35:23
@Jon錯誤來自入口控制器中的第77行,這是參數params方法中的params require行。我嘗試了下面Pavan的解決方案,但它沒有解決我的問題。 – Swappticon 2015-02-24 18:17:33