Rails - 简单形式&嵌套模型

时间:2015-06-09 10:10:19

标签: ruby-on-rails simple-form

我的Rails 4应用程序中有三个模型。

我有项目,项目问题和项目答案模型。

项目

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

项目问题有这些关联:

  belongs_to :project#, counter_cache: true
  has_many :project_answers, dependent: :destroy
  belongs_to :user
  accepts_nested_attributes_for :project_answers

项目答案协会是:

 belongs_to :project_question#, counter_cache: true  
belongs_to :user 

我的routes.rb有:

  resources :projects do
  # patch '/toggle-draft', to 'projects#toggle_draft', as: 'toggle_draft'
    resources :project_questions do
      resources :project_answers
    end
  end

在我的projects_controller中,我已经允许params用于项目问题和答案如下:

 project_question_attributes: [:title, :content, :user_id, :project_id,
      project_answer_attributes: [:answer, :project_question_id]],

这些参数也可以在项目问题和项目答案控制器中使用。

在我的项目视图中,我想渲染我在project_questions视图文件夹中的部分内容。

在项目展示页面中,我有:

  <%= link_to 'Ask a question', new_project_question_path %> <% end %>
  <%= render 'project_questions/pqps' %>

在我的project_questions部分名为_pqps中,我有;

<div class="containerfluid">
  <div class="row">
    <div class="col-md-10 col-md-offset-1">

      <% f.simple_fields_for :project_questions, @project.project_questions.build do |q| %>
          <div class="categorytitle">
            <%= q.title %>
          </div>
          <div class="generaltext">
            <%= q.content %>
          </div>
          <%= render 'project_answers/show' %>
          <span class="editproject">   <% if current_user.id ==  @project.creator_id %>
            <%= link_to 'Answer this question', new_project_answer_path %>
              <% end %>
          </span>

      <% end %>
    </div>
  </div>
</div>

然而,当我尝试这个时,我收到的错误是:

undefined local variable or method `new_project_question_path'

谁能看到我做错了什么?

谢谢

0 个答案:

没有答案