Rails:如何呈现嵌套的部分

时间:2015-06-25 03:46:00

标签: ruby-on-rails path

我正在尝试使用Rails 4和Simple Form制作应用程序。

我有一个项目模型,一个project_questions模型和一个项目答案模型。

协会是:

Project.rb:

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

Project_question.rb:

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

项目答案.rb:

  belongs_to :project_questions#, counter_cache: true
  belongs_to :user

我的路线是嵌套的:

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

我的项目展示,包括部分显示有关项目的问题。

  <%= render 'project_questions/pqps' %>

在该项目 - 部分问题(pqps)中,我试图将项目答案部分呈现,以便答案位于问题的下方。

  <% @project.project_questions.each do |singleQuestion| %>

      <div class="categorytitle">
        <%= singleQuestion.title %>

      </div>
      <div class="generaltext">
        <%= singleQuestion.try(:content) %>
      </div>
      <span class="editproject">
        <% if current_user.id == @project.creator_id %>

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

<% end %>

我很挣扎,因为我在project_answer部分视图中的代码是:

        <%= @project.project_questions.project_answers.answer %>

当我尝试这个时,我收到了这个错误:

undefined method `project_answers' for #`<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_ProjectQuestion:0x000001039ebcc8>`

我已经尝试了该项目答案代码行的多个不同位的所有形式和组合,但没有任何作用。

当我尝试答案时:             &lt;%= @ project.project_questions.project_answer.answer%&gt;

undefined method `project_answer' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_ProjectQuestion:0x00000102b59318>

我不明白为什么会这样。

在我的项目答案控制器中,我有:

class ProjectAnswersController < ApplicationController
  before_action :set_project
  before_action :set_project_question
  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 = @project_question.build_project_answer
  end

  # GET /project_answers/1/edit
  def edit
  end

  # POST /project_answers
  # POST /project_answers.json
  def create
    @project_answer = @project_question.build_project_answer(project_answer_params)


    respond_to do |format|
      if @project_answer.save
        format.html { redirect_to @project, 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
      @project = Project.find(params[:project_id])
    end

    def set_project_question
      @project_question = @project.project_questions.find(params[:project_question_id])
    end


  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, :id)
    end
end

我原本以为应该让我只使用@ project.answer.answer,但我也尝试过,它也不起作用。

任何人都可以看到我需要做些什么才能回答问题吗?

1 个答案:

答案 0 :(得分:0)

如果您正确阅读了错误

undefined method project_answers' for
#<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_ProjectQuestion:0x000001039ebcc8>

它明确指出 您在集合上调用project_answers而不是单个记录。此外,您在ProjectQuestion和ProjectAnswer模型上有1-1关联,因此它应该是project_answer而不是project_answers

修正:

根据部分内的HTML,您可以使用其中任何一个:

<%= render partial: 'project_answers/answers', locals: {some_local_variable_used_in_partial: singleQuestion.project_answer}  %>

有关详细信息,请查看using partials

编辑:

您仍然在集合上调用project_answer。您需要遍历project_questions,然后为其呈现答案。在你的部分内你需要:

<% @project.project_questions.each do |question| %>
  <%= question.project_answer.answer %>
<% end %>