Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 未定义的方法错误-单表继承-路由错误_Ruby On Rails_Ruby_Routing_Undefined_Single Table Inheritance - Fatal编程技术网

Ruby on rails 未定义的方法错误-单表继承-路由错误

Ruby on rails 未定义的方法错误-单表继承-路由错误,ruby-on-rails,ruby,routing,undefined,single-table-inheritance,Ruby On Rails,Ruby,Routing,Undefined,Single Table Inheritance,我正在创建一个单表继承模型来构建一个注释线程,我遇到了一个令人困惑的路由错误 我已设置了我的模型 class Question < ApplicationRecord has_many :answers, class_name: 'Questions::Answer' end class Answer < ApplicationRecord has_many :answers, class_name: 'Answers::Answer' end 在我看来,我提供了答案和一

我正在创建一个单表继承模型来构建一个注释线程,我遇到了一个令人困惑的路由错误

我已设置了我的模型

class Question < ApplicationRecord
  has_many :answers, class_name: 'Questions::Answer'
end

class Answer < ApplicationRecord
  has_many :answers, class_name: 'Answers::Answer'
end
在我看来,我提供了答案和一个表单来添加答案

<%= render @answers %></br>
<%= render "answers/form" %>
但是,这给了我一个未定义的方法错误:
未定义的方法'questions\u answers\u path'

在我的道路上,唯一的道路是

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_answer          GET    /answers/:id/edit(.:format)                     answers#edit
answer               GET    /answers/:id(.:format)                          answers#show         
                     PATCH  /answers/:id(.:format)                          answers#update       
                     PUT    /answers/:id(.:format)                          answers#update       
                     DELETE /answers/:id(.:format)                          answers#destroy
然后问题是,为什么它要寻找复数的
疑问词\u答案\u路径
而不是疑问词\u答案\u路径

形式相对标准:

<%= simple_form_for(@answer) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">
  <%= f.input :body %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

您必须给出显式路径的原因很明显:因为您构建了类
Questions::answer
的答案,而
简单表单只能从表单对象的类派生路径。但这是一个非正统的解决方案,用于解决线程化答案问题。为什么要用这种方式来解决问题,而不是使用指向父对象的自引用链接?我最初尝试了指向父对象的自引用链接,但无法将答案呈现在特定答案下方的正确位置。我向StackOverflow寻求帮助,并被建议使用自表继承重新建模问题。在做了一些研究之后,它似乎更适合,但正如你所看到的,我遇到了一些问题。您知道为什么即使在我更改路径后表单验证仍会抛出错误吗?
 def show
    @answer = @question.answers.new
    @answers = @question.answers.page(params[:page]).per(5)
 end
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_answer          GET    /answers/:id/edit(.:format)                     answers#edit
answer               GET    /answers/:id(.:format)                          answers#show         
                     PATCH  /answers/:id(.:format)                          answers#update       
                     PUT    /answers/:id(.:format)                          answers#update       
                     DELETE /answers/:id(.:format)                          answers#destroy
<%= simple_form_for(@answer) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">
  <%= f.input :body %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
class AnswersController < ApplicationController
  before_action :set_answer, only: [:edit, :update, :destroy]
  before_action :set_question

  def create
    @answer = @question.answers.build(answer_params)

    respond_to do |format|
      if @answer.save
        format.html { redirect_to question_path(@question) }
        format.json { render :show, status: :created, location: @answer }
      else
        format.html { render :new }
        format.json { render json: @answer.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    authorize @answer
    respond_to do |format|
      if @answer.update(answer_params)
        format.html { redirect_to question_answers_path(@question), notice: 'Answer was successfully updated.' }
        format.json { render :show, status: :ok, location: @answer }
      else
        format.html { render :edit }
        format.json { render json: @answer.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    authorize @answer
    @answer = Answers.find(params[:id])
    @answer.discard
    respond_to do |format|
      format.html { redirect_to question_answers_path(@question), notice: 'Answer was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_answer
      @answer = Answer.find(params[:id])
    end

    def answer_params
      params.fetch(:answer, {}).permit(:body, :type).merge(user_id: current_user.id, question_id: @question.id)
    end

    def set_question
      @question = Question.find(params[:question_id])
    end

end