【问题标题】:Argument Error (First argument in form cannot contain nil or be empty) for Form_forForm_for 的参数错误(表单中的第一个参数不能包含 nil 或为空)
【发布时间】:2016-08-21 01:04:39
【问题描述】:

我正在尝试制作一个测验模块,在该模块中记录每个多项选择题的答案并追踪到相应的用户。由于编辑视图和新视图都将使用相同的表单,因此我使用的是部分视图。

当我尝试查看新视图时,我收到一个参数错误,提示“表单中的第一个参数不能包含 nil 或为空”

控制器是:

class QuizBsController < ApplicationController
  before_action :require_sign_in

  def show
    @quiz_bs = QuizBs.find(params[:id])
  end

  def new
    @quiz_bs = QuizBs.new
  end

  def create
    @quiz_bs = QuizBs.new

    @quiz_bs.bs01 = params[:quiz_bs][:bs01]
    @quiz_bs.bs02 = params[:quiz_bs][:bs02]
    @quiz_bs.bs03 = params[:quiz_bs][:bs03]
    @quiz_bs.bs04 = params[:quiz_bs][:bs04]
    @quiz_bs.bs05 = params[:quiz_bs][:bs05]
    @quiz_bs.bs06 = params[:quiz_bs][:bs06]

    @quiz_bs.user = current_user

    if quiz_bs.save
      flash[:notice] = "Quiz results saved successfully."
      redirect_to user_path(current_user)
    else
      flash[:alert] = "Sorry, your quiz results failed to save."
      redirect_to welcome_index_path
    end
  end

  def update
    @quiz_bs = QuizBs.find(params[:quiz_bs])

    @quiz_bs.assign_attributes(quiz_bs_params)

    if @quiz_bs.save
      flash[:notice] = "Post was updated successfully."
      redirect_to user_path(current_user)
    else
      flash.now[:alert] = "There was an error saving the post. Please try again."
      redirect_to welcome_index_path
    end
  end

  private
  def quiz_bs_params
    params.require(:quiz_bs).permit(:bs01, :bs02, :bs03, :bs04, :bs05, :bs06)
  end

end

新视图是:

<div class="container">
  <div id="quiz_bs_new" class="text-center">
    <h1>Body Structure Quiz</h1>
  </div>

<%= render partial: "quiz", locals: { quiz: @quiz_bs } %>

</div>
<div class="buffer-50"></div>

这是我的部分表单:

<%= form_for @quiz do |f| %>
  <div class="row">
      <div class="col-sm-12">

        <div class="form-group">
          <%= f.label "Question Text" %><br>
          <div class="radio left-padding-30">
            <%= f.radio_button :bs01, '1'%><label>31 Seconds or More</label><br>
            <%= f.radio_button :bs01, '2'%><label>26 - 30 Seconds</label><br>
            <%= f.radio_button :bs01, '3'%><label>21 - 25 Seconds</label><br>
            <%= f.radio_button :bs01, '4'%><label>16 - 20 Seconds</label><br>
            <%= f.radio_button :bs01, '5'%><label>10 - 15 Seconds</label><br>
            <%= f.radio_button :bs01, '6'%><label>6 - 9 Seconds</label><br>
            <%= f.radio_button :bs01, '7'%><label>0 - 5 Seconds</label>
          </div>
        </div>

    </div> <!-- column end -->
  </div> <!-- row end -->

    <div class="buffer-25"></div>

  <div class="row">
    <div class="col-xs-12 center-block">
        <div class="center-block"><%= f.submit %></div>
      </div> <!-- column end -->
  </div> <!-- row end -->

<% end %>

【问题讨论】:

  • ist updatenew ?也发布你的rake routes,这就是修复。

标签: ruby-on-rails erb partials


【解决方案1】:

变量作为局部变量传递给局部变量。在您的表单中使用 quiz 而不是 @quiz 应该可以解决问题。

编辑

还在你的 form_for 中设置 url 以确保表单路由到正确的控制器操作:

form_for quiz, url: quiz_bs_path, method: :post

并用映射到 QuizBsController#create 的任何路由替换 quiz_bs_path

【讨论】:

  • 这至少改变了错误。现在我收到一个无方法错误说“未定义的方法`quiz_bs_index_path'”...
  • 同一个:&lt;%= form_for quiz do |f| %&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多