我有用户/微博/评论模型,用户可以在其中评论其他人的微博。在每个帖子下都会显示一个文本字段,以便用户可以输入评论,但是我很难找到 Micropost Id。我认为问题出在我的 form_for 评论或控制器中,但我不确定。希望得到一些帮助,谢谢。
错误:找不到没有 ID 的微博
楷模:
User Model: has many microposts, has many comments
Micropost Model: belongs to user, has many comments
Comment Model: belongs to micropost, belongs to user
用户控制器:
def show #(the profile page where all the posts and comments are)
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
@micropost = current_user.microposts.build if signed_in?
@comments = @micropost.comments
@comment = current_user.comments.build(:micropost => @micropost) if signed_in?
end
评论控制器:
def create
@micropost = Micropost.find(params[:id])
@comment = current_user.comments.build(:micropost => @micropost) #can someone explain what happens in the parentheses?
@comment.user = current_user
@comment.save
redirect_to :back
end
查看/评论/_comment_form:
<%= form_for(@comment) do |f| %>
<div id="comment_field">
<%= f.text_field :content, placeholder: "Say Something..." %>
</div>
<% end %>
路线:
resources :users
resources :microposts, only: [:create, :destroy]
resources :comments, only: [:create, :destroy]