没有路线匹配[DELETE]“/ questions / 5 / answers”

时间:2017-05-26 16:02:36

标签: ruby-on-rails routing associations rails-activerecord model-associations

尝试创建删除按钮,但路径存在一些问题。我以为我把它写得正确了。非常感谢任何帮助和建议

  

错误:没有路线匹配[DELETE]“/ questions / 5 / answers”

<%= button_to "delete", question_answers_path(@question, @answer), method: :delete, class: "btn btn-default", data: {confirm: "Are you sure you want to delete this answer?"} %>

耙路线

Controller#Action
    question_answers GET    /questions/:question_id/answers(.:format)          answers#index
                     POST   /questions/:question_id/answers(.:format)          answers#create
 new_question_answer GET    /questions/:question_id/answers/new(.:format)      answers#new
edit_question_answer GET    /questions/:question_id/answers/:id/edit(.:format) answers#edit
     question_answer GET    /questions/:question_id/answers/:id(.:format)      answers#show
                     PATCH  /questions/:question_id/answers/:id(.:format)      answers#update
                     PUT    /questions/:question_id/answers/:id(.:format)      answers#update
                     DELETE /questions/:question_id/answers/:id(.:format)      answers#destroy
       vote_question POST   /questions/:id/vote(.:format)                      questions#vote
           questions GET    /questions(.:format)                               questions#index
                     POST   /questions(.:format)                               questions#create
        new_question GET    /questions/new(.:format)                           questions#new
       edit_question GET    /questions/:id/edit(.:format)                      questions#edit
            question GET    /questions/:id(.:format)                           questions#show
                     PATCH  /questions/:id(.:format)                           questions#update
                     PUT    /questions/:id(.:format)                           questions#update
                     DELETE /questions/:id(.:format)                           questions#destroy

路由

  resources :questions do
    resources :answers
    member { post :vote }
  end

QuestionsController

class QuestionsController < ApplicationController
  before_action :authenticate_user!, except: [:index, :show, :home]
  before_action :find_question, only: [:edit, :update, :destroy, :vote]

  # GET /questions
  # GET /questions.json
  def index
    if signed_in?
      # @question = Question.find(params[:question_id] || params[:id])
      @questions = Question.all
    else
      respond_to do |format|
        format.html { redirect_to root_path, notice: 'Must Sign In To Start Having Fun.' }
        format.json { head :no_content }
        end
    end
  end

  def home

  end
  # GET /questions/1
  # GET /questions/1.json
  def show
    @question = Question.find(params[:question_id] || params[:id])
    @answer = Answer.new
    # @answers = @qustion.answers.ordered_by_creation
  end

  # GET /questions/new
  def new
    @question = Question.new

  end

  def vote
    value = params[:type] == "up" ? 1 : -1
    @question = Question.find(params[:id])
    @question.add_or_update_evaluation(:votes, value, current_user)
    redirect_to :back, notice: "Thank you for voting!"
  end
  # GET /questions/1/edit
  def edit
      @question = Question.find(params[:id])
  end

  # POST /questions
  # POST /questions.json
  def create
    @question = current_user.questions.new(question_params)
    # @question = Question.new(question_params)
    respond_to do |format|
      if @question.save
        format.html { redirect_to @question, notice: 'Question was successfully created.' }
        format.json { render :show, status: :created, location: @question }
      else
        format.html { render :new }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /questions/1
  # PATCH/PUT /questions/1.json
  def update
    respond_to do |format|
      if @question.update(question_params)
        format.html { redirect_to @question, notice: 'Question was successfully updated.' }
        format.json { render :show, status: :ok, location: @question }
      else
        format.html { render :edit }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /questions/1
  # DELETE /questions/1.json
  def destroy
    @question.destroy
    respond_to do |format|
      format.html { redirect_to questions_url, notice: 'Question was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_question
      @question = Question.find(params[:id])
    end

    def find_question
      @question = current_user.questions.find_by_id(params[:id])
      redirect_to root_path, alert: "Access Denied" unless @question
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def question_params
      params.require(:question).permit(:question, :question_id, :vote, {category_ids: []})
    end

end

AnswersController

class AnswersController < ApplicationController

  def create
    @answer = @question.answers.new(answer_attributes)
    @answer.user = current_user
    if @answer.save
      redirect_to @question, noticee: "Answer created successfully."
    else
      render "/questions/show"
    end
  end

  def destroy
    @answer = @questions.answers.find(params[:id])
    if @answer.user = current_user && @answer.destroy
      respond_to do |format|
        format.html { redirect_to question_url, notice: 'Answer was successfully destroyed.' }
        format.json { head :no_content }
      end
    else
      redirect_to @question, error: "We had trouble deleting the answer"
    end
  end

  private

  def answer_params
    params.require(:answer).permit(:user_id, :question_id, :body)
  end


end

如果它有帮助,这些是模型

模型

class Question < ActiveRecord::Base
  belongs_to :user
  has_many :answers, dependent: :destroy
  has_many :categorizations, dependent: :destroy
  has_many :categories, through: :categorizations
end


class Answer < ActiveRecord::Base
  belongs_to :question

  validates_presence_of :body

  scope :ordered_by_creation, -> { order("created_at DESC")}
end

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :answers
  has_many :questions, :dependent => :destroy
end

2 个答案:

答案 0 :(得分:0)

您没有使用正确的路径助手。

question_answer DELETE /questions/:question_id/answers/:id(.:format)      answers#destroy

question_answers_path替换为question_answer_path

<%= button_to "delete", question_answer_path(@question, @answer), method: :delete, class: "btn btn-default", data: { confirm: "Are you sure you want to delete this answer?" } %>

答案 1 :(得分:0)

我已经创建了测试应用。 这就是我的解决方案。 的routes.rb

resources :questions do
  resources :answers
end

answers_controllers.rb

def index
  @answers = Answer.all
  @question = Question.find(params[:question_id])
end

  def destroy
    @answer = Answer.find(params[:id])
    @question = @answer.question
    if @answer.user = current_user && @answer.destroy
      respond_to do |format|
        format.html { redirect_to @question, notice: 'Answer was successfully destroyed.' }
        format.json { head :no_content }
      end
    else
      redirect_to @question, error: "We had trouble deleting the answer"
    end

answers.html.erb

<% @answers.each do |answer| %>
  <%= button_to 'Destroy', question_answer_path(@question, answer), method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>