AbstractController::ActionNotFound form_tag 在 Rails

AbstractController::ActionNotFound with form_tag in Rails

当我如下使用 form_tag 时,出现此错误:

AbstractController::ActionNotFound at /quizzes/[whichever_quiz_id]

The action 'whichever_quiz_id' could not be found for QuizzesController

我的代码如下:

erb文件

<%= form_tag action: :add_questions  do %>
  <%= collection_select(:quiz, :id, Quiz.where.not(id: params[:id]), :name, :id, prompt: 'Select Quiz') %>
  <%= text_field_tag(:input_quiz_questions, 'Question ids') %>
  <%= submit_tag "Add" %>
<% end %>

所以我的 QuizController 中有一个名为 add_questions 的方法,它从另一个测验中选择一个问题并将其添加到当前测验中。

def add_questions
  id = params[:id]
  required_quiz_id = params[:quiz][:id].to_i #taken from collection_select
  required_questions_ids = params[:input_quiz_questions].split().map { |s| s.to_i } #taken from text_field_tag
  # remaining logic here

路线

resources :quizzes, as: 'tests' do
  member do
    get :add_questions
  end
end

那么,为什么会出现上述错误,我该如何纠正?

您应该在 form_tag 之后写 url,而不是选项散列中的操作名称

<%= form_tag quizzes_add_questions_path, method: :get  do %>