0
對於我的應用程序,我目前在用戶的頁面上列出了與用戶相關的項目。對於列出的每個項目,我想渲染爲每個項目所做的評論。我可以通過表單向每個項目發表評論,但無法呈現與評論相關的評論。沒有呈現。我一直在玩users_controller,認爲它沒有成功。我如何解決它?Rails - 爲什麼我的評論不呈現?
我已經爲用戶,項目和評論創建了模型和控制器。屬於項目和項目的評論屬於用戶。
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 %>
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association –
在routes.rb中,我想你的第一個塊就足夠了。在projects.html.erb中,此渲染假設要做什麼?在users_controller中,你正在一個接一個地定義@project 2次......這似乎並不是故意的。 – Raindal
您正在使用相同的參數[:id]來查找您的用戶和@commemts的項目。 – Edward