0
现在,我有两种模型:User和Micropost。用户模型使用Devise。文件的创建属于它们各自的微博和用户的评论(在使用Devise的应用程序中)
实施例涉及:
user_controller.html.erb:
class PagesController < ApplicationController
def index
@user = current_user
@microposts = @user.microposts
end
end
index.html.erb:
<h2>Pages index</h2>
<p>email <%= @user.email %></p>
<p>microposts <%= render @microposts %></p>
微柱/ _micropost .html.erb
<p><%= micropost.content %></p>
micropost.rb:
class Micropost < ActiveRecord::Base
attr_accessible :content
belongs_to :user
end
user.rg:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :microposts
end
现在我要为微柱创建注释:
- 每条评论应该属于其各自的微博和用户(评论者)。不知道如何做到这一点(是否使用多态关联?)。
- 一个用户应该有很多微博和评论(不知道该怎么做)。
- 我不知道如何使它成为当前登录用户的评论(我想我必须对Devise的
current_user
做些什么)。
任何建议来完成此? (对不起,我是Rails初学者)