Rails - 为什么我的评论没有呈现?

时间:2013-05-15 04:18:33

标签: ruby-on-rails

对于我的应用程序,我目前正在用户页面上列出与用户相关的项目。对于列出的每个项目,我想呈现为每个项目创建的注释。我可以通过表单向每个项目发布评论到数据库,但无法呈现与评论相关的评论。什么都没有渲染。我一直在玩user_controller,认为它没有成功。我该如何解决?

我为用户,项目和评论创建了模型和控制器。注释属于项目和项目属于用户。

schema.rb

create_table "comments", :force => true do |t|
  t.integer  "user_id"
  t.integer  "project_id"
  t.text     "content"
  t.datetime "created_at",    :null => false
  t.datetime "updated_at",    :null => false
end

user.rb

has_many :projects
has_many :comments

project.rb

has_many :comments
belongs_to :user

comment.rb

belongs_to :project

的routes.rb

resources :users 

resources :projects do
  resources :comments 
end

resources: comments

查看/用户/ _projects.html.erb

<%= render @projects %>

users_controller.rb

def comments
  @user = User.find(params[:id])
  @projects = @user.projects.newest.page(params[:comments_page]).per_page(10)
  @project = Project.new
  @comments = Project.find(params[:id]).comments.newest.page(params[:comments_page]).per_page(2)
end

查看/项目/ _project.html.erb

<%= project.content %>
<%= render 'comments/form', project:project %>
<%= render @comments %>
<%= will_paginate @comments, :param_name => 'comments_page' %> 

查看/评论/ _comment.html.erb

<%= comment.content %>

1 个答案:

答案 0 :(得分:0)

在project.html.erb文件中尝试使用如下循环:

<% project.comments.each do |c| %>
  #do something
<% end %>