Rails - 简单表单 - 嵌套资源路径

时间:2015-06-23 06:49:04

标签: ruby-on-rails simple-form simple-form-for

我正在尝试使用简单的表单在Rails 4中创建一个应用程序。

我有3个模型:Project,Project_Question,Project_Answer

协会是:

项目:

 has_many :project_questions, dependent: :destroy#, through: :projects
  accepts_nested_attributes_for :project_questions

项目问题:

belongs_to :project#, counter_cache: true
  has_one :project_answer, dependent: :destroy
  belongs_to :user
  accepts_nested_attributes_for :project_answer

项目答案:

  belongs_to :project_question#, counter_cache: true
  belongs_to :user

我的路线嵌套如下:

resources :projects do
  resources :project_questions do
    resources :project_answers
  end
end

在我的项目问题部分中,我想要一个链接来回答问题。我有一个链接,如下所示:

        <%= link_to 'Answer this question', new_project_project_question_project_answer_path(:project_id => @project.id, :project_question_id => singleQuestion.id) %>

我得到了帮助,在附带问题的帮助下找到链接点:

http://stackoverflow.com/questions/30978447/rails-link-to-path

我改变了项目答案模型,以便项目问题有一个(而不是有很多项目答案)并更新了回答此问题链接中的路径,以单数而不是复数。

当我点击“回答此问题”链接时,我收到此错误:

undefined method `project_answers_path' 

它指向项目答案表单中的行的简单形式。那个表格有:

<%= simple_form_for [@project, @project_question, @project_answer] do |f| %>
    <%= f.input :project_question_id, as: :hidden, input_html: {value: @project_question.id} %>
    <%= f.input :answer, label: 'Answer?', :label_html => {:class => 'question-project'}, placeholder: 'Type your answer here', :input_html => {:style => 'width: 650px', class: 'response-project'} %>
    <br><br><br>
    <%= f.button :submit, 'Send!', :class => "cpb" %>

<% end %>

当我获取项目答案的路线时,我得到:

    project_project_question_project_answers GET      /projects/:project_id/project_questions/:project_question_id/project_answers(.:format)          project_answers#index
                                              POST     /projects/:project_id/project_questions/:project_question_id/project_answers(.:format)          project_answers#create
  new_project_project_question_project_answer GET      /projects/:project_id/project_questions/:project_question_id/project_answers/new(.:format)      project_answers#new
 edit_project_project_question_project_answer GET      /projects/:project_id/project_questions/:project_question_id/project_answers/:id/edit(.:format) project_answers#edit
      project_project_question_project_answer GET      /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format)      project_answers#show
                                              PATCH    /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format)      project_answers#update
                                              PUT      /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format)      project_answers#update
                                              DELETE   /projects/:project_id/project_questions/:project_question_id/project_answers/:id(.:format)      project_answers#destroy

我不明白为什么当只有一个答案时,project_project_questions_project_answers操作的#new动作是多个的答案,但我认为这与我点击“答案”时收到的错误信息有关问题'链接。

new_project_project_question_project_answer GET      /projects/:project_id/project_questions/:project_question_id/project_answers/new(.:format)      project_answers#new
 e

当我尝试复制表单第一行的项目答案时,对于项目问题,如下:

<%= simple_form_for [@project, @project_questions, @project_answer] do |f| %>

我收到此错误:

undefined method `project_answers_path' for #<#<Class:0x0000010a193f10>:0x0000010a3abb40>

当我尝试复制表单第一行的项目答案,项目问题和项目答案时,如下:

<%= simple_form_for [@project, @project_questions, @project_answers] do |f| %>

我收到此错误:

undefined method `model_name' for NilClass:Class

谁能看到我做错了什么?我读了一篇帖子,建议我在routes.rb中使用project_answer(singular)。我试过这个,但是我得到一个错误,说没有路由匹配复数。

项目答案控制器:

class ProjectAnswersController < ApplicationController
  before_action :set_project_answer, only: [:show, :edit, :update, :destroy]

  # GET /project_answers
  # GET /project_answers.json
  def index
    @project_answers = ProjectAnswer.all
  end

  # GET /project_answers/1
  # GET /project_answers/1.json
  def show
  end

  # GET /project_answers/new
  def new
    @project_answer = ProjectAnswer.new
  end

  # GET /project_answers/1/edit
  def edit
  end

  # POST /project_answers
  # POST /project_answers.json
  def create
    @project_answer = ProjectAnswer.new(project_answer_params)

    respond_to do |format|
      if @project_answer.save
        format.html { redirect_to @project_answer, notice: 'Project answer was successfully created.' }
        format.json { render action: 'show', status: :created, location: @project_answer }
      else
        format.html { render action: 'new' }
        format.json { render json: @project_answer.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /project_answers/1
  # PATCH/PUT /project_answers/1.json
  def update
    respond_to do |format|
      if @project_answer.update(project_answer_params)
        format.html { redirect_to @project_answer, notice: 'Project answer was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @project_answer.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /project_answers/1
  # DELETE /project_answers/1.json
  def destroy
    @project_answer.destroy
    respond_to do |format|
      format.html { redirect_to project_answers_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_answer_params
      params[:project_answer].permit(:answer, :project_question_id)
    end
end

1 个答案:

答案 0 :(得分:0)

project_answers 控制器的new操作应该@project@project_question已定义

def new
  @project_answer = ProjectAnswer.new
  @project = Project.find(params[:project_id])
  @project_question = ProjectQuestion.find(params[:project_question_id])
end

这样你就可以像这样使用那些

<%= simple_form_for [@project, @project_question, @project_answer] do |f| %>