路由错误:没有路由匹配[POST]“/ flex_quiz / new”

时间:2016-04-30 13:30:49

标签: ruby-on-rails forms routes partials

在我的rails应用中,我正在尝试使用部分表单在newedit视图上显示相同的测验。我可以看到新的视图页面,但是当我点击<%= f.submit "Submit Answers" %>时,我收到错误No route matches [POST] "/flex_quiz/new"

以下是我的部分中的form for行:

<%= form_for @flex_quiz, url: url do |f| %>

以下是本地人在new视图中的表现:

 <%= render partial: "quiz", locals: { url: new_flex_quiz_path, method: :post } %>

我的edit观点:

<%= render "quiz", url: edit_flex_quiz_path(@flex_quiz), method: :put  %>

以下是路径路径:

         Prefix Verb   URI Pattern                    Controller#Action
            ...     
flex_quiz_index GET    /flex_quiz(.:format)           flex_quiz#index
                POST   /flex_quiz(.:format)           flex_quiz#create
  new_flex_quiz GET    /flex_quiz/new(.:format)       flex_quiz#new
 edit_flex_quiz GET    /flex_quiz/:id/edit(.:format)  flex_quiz#edit
      flex_quiz GET    /flex_quiz/:id(.:format)       flex_quiz#show
                PATCH  /flex_quiz/:id(.:format)       flex_quiz#update
                PUT    /flex_quiz/:id(.:format)       flex_quiz#update
                DELETE /flex_quiz/:id(.:format)       flex_quiz#destroy

有人可以建议如何解决这个问题吗?我查看了几个类似的帖子(例如thisthis),但由于我使用的是偏序,因此这里的解决方案必须有所不同。

修改

以下是我flex_quiz_controller中的定义:

class FlexQuizController < ApplicationController
before_action :require_sign_in

def show
  @flex_quiz = FlexQuiz.find(params[:id])
end

def new
  @flex_quiz = current_user.build_flex_quiz
end

def create
  @flex_quiz = FlexQuiz.new

  @flex_quiz.flex01 = params[:flex_quiz][:flex01]
  @flex_quiz.flex02 = params[:flex_quiz][:flex02]
  @flex_quiz.flex03 = params[:flex_quiz][:flex03]
  @flex_quiz.flex04 = params[:flex_quiz][:flex04]
  @flex_quiz.flex05 = params[:flex_quiz][:flex05]
  @flex_quiz.flex06 = params[:flex_quiz][:flex06]
  @flex_quiz.flex07 = params[:flex_quiz][:flex07]
  @flex_quiz.flex08 = params[:flex_quiz][:flex08]
  @flex_quiz.flex09 = params[:flex_quiz][:flex09]
  @flex_quiz.flex10 = params[:flex_quiz][:flex10]

  @flex_quiz.user = current_user

  if @flex_quiz.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 edit
  @flex_quiz = FlexQuiz.find(params[:id])
end

def update
  @flex_quiz = FlexQuiz.find(params[:id])

  @flex_quiz.assign_attributes(flex_quiz_params)

  if @flex_quiz.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 flex_quiz_params
  params.require(:flex_quiz).permit(:flex01, :flex02, :flex03, :flex04, :flex05, :flex06, :flex07, :flex08, :flex09, :flex10)
end

end

2 个答案:

答案 0 :(得分:4)

如果您想要创建新的flex_quiz个对象,那么您需要POSTflex_quiz_index_path

请注意,在路径路径中,如果查看new_flex_quiz,则HTTP谓词为GET

可能有点不直观,但new操作实际上是GET请求。

应该在其中创建对象的操作是create操作。

所以要解决你的问题,这应该可以解决问题:

<%= render partial: "quiz", locals: { url: flex_quiz_index_path, method: :post } %>

编辑

  1. 您可以简单地在form_for中定义表单,而不是定义本地化:

  2. 您还必须在控制器操作中定义@flex_quiz(在您的情况下为neweditform_for会自动推断出相应的网址。

    来自documentation

      

    但是,如果传递给记录,则可以进一步简化   form_for是一种资源,即它对应于一组RESTful   路线,例如使用config / routes.rb中的resources方法定义。   在这种情况下,Rails将简单地推断出相应的URL   记录自己。

    1. 您还需要将命名从单数形式更改为复数形式。

答案 1 :(得分:0)

做可恢复表格的轨道是:

应用程序/视图/ flex_quiz / _form.html.erb:

<%= form_for(@flex_quiz) do |f| %>
  # ...
<% end %>

应用程序/视图/ flex_quiz / new.erb:

<h1>Create a new quiz</h1>
<%= render 'form' %>

应用程序/视图/ flex_quiz / edit.erb:

<h1>Edit a quiz</h1>
<%= render 'form' %>

虽然使用本地人通常是个好主意,但这里不需要它。请注意,我们只是将资源而不是URL传递给form_for - 这是约定优于配置的实际操作,这也是Rails非常棒的原因。

Rails自行计算出用于action属性的URL以及基于资源是否已保存的方法。

然而,为了让您真正遵循惯例。确保使用正确的复数形式(the plural of quiz is quizzes):

resources :flex_quizzes

class FlexQuizzesController < ApplicationController
end

不幸的是,当涉及到其他设置时,您需要重新访问绘图板。认为你可以用一个模型做到这一点并不现实。你通常会有几个关系模型:

class Quiz
  has_many :questions
end

class Question
  belongs_to :quiz
  has_many :answers
end

class Answer
  belongs_to :question
end

class UserQuiz
  belongs_to :user
  belongs_to :quiz
end

class UserAnswer
  belongs_to :question
  belongs_to :answer
end

您可以使用一个或多个控制器让管理员创建测验,并使用单独的控制器让用户回答测验。它是一个非常常见的领域,所以你应该能够找到很多例子。