我是编程新手,我正在努力应用我在我的应用中实现的新功能。我希望用户能够评论他人的微博。允许用户评论帖子,不能批量指定帖子
,我发现了错误:斜面质量分配微柱
用户模型:
attr_accessible :name, :email, :password, :password_confirmation #is this secure with password there?
attr_protected :admin #attr_protected necessary?
has_many :microposts, dependent: :destroy
has_many :comments, :through => :microposts, dependent: :destroy
型号微柱:
attr_accessible :comment #basically the content of the post
attr_protected :user_id
has_many :comments, dependent: :destroy
评论模型:
attr_accessible :content, :micropost
belongs_to :user
belongs_to :micropost
validates :user_id, presence: true
validates :micropost_id, presence: true
validates :content, presence: true
default_scope order: 'comments.created_at ASC' #is this necessary?
评论控制研究ER:
def create
@micropost = Micropost.find_by_id(params[:id]) #is this necessary?
@comment = current_user.comments.create(:micropost => @micropost)
redirect_to :back
end
用户控制器:
def show
@user = User.find_by_id(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
@micropost = current_user.microposts.build
@comments = @micropost.comments
@comment = current_user.comments.create(:micropost => @micropost) #build, new or create??
end
查看/评论/ _form:(这部分被称为在每个微柱的末端)
<span class="content"><%= @comment.content %></span>
<span class="timestamp">Said <%= time_ago_in_words(@comment.created_at) %> ago.</span
<%= form_for(@comment) do |f| %>
<%= f.text_field :content, placeholder: "Say Something..." if signed_in? %>
<% end %>
路线:
resources :users
resources :microposts, only: [:create, :destroy]
resources :comments, only: [:create, :destroy]
因此,这意味着我应该删除所有的attr_protected?此外,micropost不是微博模式的一部分。我添加了您的建议,但遇到了同样的问题。 – Jaqx
您能否介绍一下其他错误的更多细节? – JMarques
我相信你必须在Micropost模型上有“attr_accessible:comment_id”。 – JMarques