对于我的应用程序,我目前在用户页面上列出与用户相关的项目。对于列出的每个项目,我想呈现为每个项目所做的评论。我可以通过表单将每个项目的评论发布到数据库中,但无法呈现与评论相关的评论。没有渲染。我一直在玩 users_controller 认为它没有成功。我如何解决它?
我为用户、项目和评论创建了模型和控制器。评论属于项目,项目属于用户。
架构.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
用户.rb
has_many :projects
has_many :comments
项目.rb
has_many :comments
belongs_to :user
评论.rb
belongs_to :project
路线.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 %>