无法通过' id' =编辑找到PerQuiz

时间:2016-09-08 21:34:03

标签: ruby-on-rails

我在几个项目中遇到过这个错误,似乎无法找到出路。我试图访问我的per_quiz#edit视图,但它正在寻找show per_quiz id的"编辑和"编辑& #34;

我的路线只是resources :per_quizzes而我的per_quizzes_controller是这样的:

class PerQuizzesController < ApplicationController
  before_action :require_sign_in

    def show
      @per_quiz = PerQuiz.find(params[:id])
    end

    def new
      @per_quiz = current_user.build_per_quiz
    end

    def create
      @per_quiz = PerQuiz.new

      @per_quiz.iper_code = params[:per_quiz][:iper_code]
      @per_quiz.myper_code = params[:per_quiz][:myper_code]


      @per_quiz.user = current_user

      if @per_quiz.save
        flash[:notice] = "Your personality saved successfully."
        redirect_to user_path(current_user)
      else
        flash[:alert] = "Sorry, your results failed to save."
        redirect_to welcome_index_path
      end
    end

    def edit
      @per_quiz = PerQuiz.find(params[:id])
    end

    def update
      @per_quiz = PerQuiz.find(params[:id])

      @per_quiz.assign_attributes(per_quiz_params)

      if @per_quiz.save
        flash[:notice] = "Results were updated successfully."
        redirect_to user_path(current_user)
      else
        flash.now[:alert] = "There was an error saving your results. Please try again."
        redirect_to welcome_index_path
      end
    end

    private
    def per_quiz_params
      params.require(:per_quiz).permit(:iper_code, :myper_code)
    end
end

我没有通过链接进入,而是手动输入url,所以我不认为它是一个链接问题。谁能澄清为什么会出现这个错误?我已经查看了很多SO帖子,无法掌握它。

1 个答案:

答案 0 :(得分:0)

我要提到的是,您必须根据rails约定将资源ID传递给您的编辑或更新。

per_quizzes/:id/edit

或者,如果您使用rails帮助程序创建编辑链接,传递资源或资源ID,它将为您创建编辑链接。

<%= link_to "Edit", edit_per_quiz_path(@per_quiz) %>