在Rails中渲染部分 - 嵌套对象

时间:2015-06-21 03:52:34

标签: ruby-on-rails

我正试图在我的rails应用程序中渲染部分内容。

我已经成功完成了其他部分功能,但是那些有效的功能与此功能之间的区别在于这部分功能属于嵌套模型。它嵌套在模型的视图中,我试图用它来渲染部分。

我有两个模型,Project和Project_question。

项目有很多项目问题,并接受项目问题的嵌套属性。

项目问题属于Project。

我的路线是:

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

我的项目问题控制器有:

class ProjectQuestionsController < ApplicationController
  before_action :set_project_question, only: [:show, :edit, :update, :destroy]

  # GET /project_questions
  # GET /project_questions.json
  def index
    @project_questions = ProjectQuestion.all
  end

  # GET /project_questions/1
  # GET /project_questions/1.json
  def show


  end

  # GET /project_questions/new
  def new
    @project_question = ProjectQuestion.new
    @project = Project.find(params[:project_id])
    # @project_id = params[:project_id]
    @project_question.project_answers[0] = ProjectAnswer.new

  end

  # GET /project_questions/1/edit
  def edit
  end

  # POST /project_questions
  # POST /project_questions.json
  def create
    @project_question = ProjectQuestion.new(project_question_params)
    @project_question.project_id = project_question_params[:project_id]


    respond_to do |format|
      if @project_question.save
        format.html { redirect_to project_url(Project.find(project_question_params[:project_id])), notice: 'Project question was successfully created.' }
        format.json { render action: 'show', status: :created, location: @project_question }
      else
        format.html { render action: 'new' }
        format.json { render json: @project_question.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /project_questions/1
  # DELETE /project_questions/1.json
  def destroy
    @project_question.destroy
    respond_to do |format|
      format.html { redirect_to project_questions_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def project_question_params
      params[:project_question].permit(:id, :title, :content, :project_id, :user_id,
      project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id]
      )
    end
end

我的项目问题表格有:

<%= simple_form_for [@project, @project_question] do |f| %>
          <%= f.input :project_id, as: :hidden, input_html: {value: @project.id} %>
          <%= f.input :title, label: 'Question:',  :label_html => {:class => 'question-title'}, placeholder: 'Type your question here', :input_html => {:style => 'width: 100%', :rows => 4, class: 'response-project'} %>
          <%= f.input :content, label: 'Is there any context or other information?', :label_html => {:class => 'question-title'}, placeholder: 'Context might help to answer your question', :input_html => {:style => 'width: 100%', :rows => 5, class: 'response-project'} %>

  <br><br><br>
      <%= f.button :submit, 'Send!', :class => "cpb" %>

我的项目问题部分有:

  </div>
  <div class="generaltext">
    <%= @project.project_question.try(:content) %>
  </div>

在我的项目#show中,我尝试按如下方式呈现保护问题部分:

<%= render 'project_questions/pqps' %>

并且project_questions / _pqps.html.erb文件包含:

<div class="containerfluid">
  <div class="row">
    <div class="col-md-10 col-md-offset-1">
          <div class="categorytitle">
            <%= @project.project_question.title %>

          </div>
          <div class="generaltext">
            <%= @project.project_question.try(:content) %>
          </div>
          <span class="editproject">
            <% if current_user.id == @project.creator_id %>
            <%= link_to 'Answer this question', new_project_question_project_answer_path(:project_quesetion_id => @project_question.id) %>
            <% end %>
          </span>
    </div>
  </div>
</div>

我尝试时收到的错误消息是:

undefined method `project_question' for #<Project:0x0000010d8bad40>

1 个答案:

答案 0 :(得分:0)

我的问题是项目和项目问题之间的关系。我不得不制作一个循环来展示每个问题。

这对我有用:

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

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

      </div>
      <div class="generaltext">
        <%= singleQuestion.try(:content) %>
      </div>