简体   繁体   English

Rails:将评论ID与特定的Micropost ID匹配

[英]Rails: Matching Comment id to Specific Micropost id

Currently I am have a comment which belongs to a micropost, but the issue is, when a user creates a comment, the comment gets stored in the database with a micropost id but the id is not for the specific micropost rather it seem as though the comment just incremented the micropost id by + 1. Very confused and would very much appreciate any help. 目前,我有一个属于微博的评论,但问题是,当用户创建评论时,该评论会存储在数据库中,并带有微博ID,但该ID不适用于特定的微博,而好像comment只是将micropost ID加+1。非常困惑,非常感谢您的帮助。 Thank you! 谢谢!

Comment Model 评论模型

class Comment < ActiveRecord::Base
  attr_accessible :content, :user_id, :micropost_id
  belongs_to :micropost
  belongs_to :user

  validates :content, presence: true, length: { maximum: 140 }


  default_scope order: 'comments.created_at DESC'
end

Micropost Model 微邮模型

class Micropost < ActiveRecord::Base
  attr_accessible :title, :content, :view_count
  belongs_to :user
  has_many :comments
  accepts_nested_attributes_for :comments
end

Comments Controller 评论控制器

class CommentsController < ApplicationController 
  def create
    @micropost = Micropost.find(params[:micropost_id])
    @comment = @micropost.comments.build(params[:comment])
    @comment.user_id = current_user.id
    @comment.save 
      respond_to do |format|
      format.html 
      format.js
    end
  end
end

Form 形成

<div class="CommentField">
<%= form_for ([@micropost, @micropost.comments.new]) do |f| %>
<%= f.text_area :content, :class => "CommentText", :placeholder => "Write a Comment..." %>
<div class="CommentButtonContainer">
<%= f.submit "Comment", :class => "CommentButton b1" %>
</div>
<% end %>
</div>

Routes 路线

  resources :microposts do
    resources :comments
  end

Raked Routes 倾斜路线

micropost_comments     GET    /microposts/:micropost_id/comments(.:format)          comments#index
                       POST   /microposts/:micropost_id/comments(.:format)          comments#create
 new_micropost_comment GET    /microposts/:micropost_id/comments/new(.:format)      comments#new
edit_micropost_comment GET    /microposts/:micropost_id/comments/:id/edit(.:format) comments#edit
     micropost_comment GET    /microposts/:micropost_id/comments/:id(.:format)      comments#show
                       PUT    /microposts/:micropost_id/comments/:id(.:format)      comments#update
                       DELETE /microposts/:micropost_id/comments/:id(.:format)      comments#destroy

I think the issue here is how much work you are putting into this. 我认为这里的问题是您为此付出了多少工作。 Rails is built to know about most of this without the need to do what you are doing. Rails旨在了解其中的大部分内容,而无需执行您正在做的事情。 My suggestion would be to change your comments controller to something like this 我的建议是将您的评论控制器更改为这样的内容

class CommentsController < ApplicationController 
  def create
    @comment = Comment.new(params[:comment])
    @comment.save 
      respond_to do |format|
      format.html 
      format.js
    end
  end
end

since you are rendering your comments partial form through another partial you'll need to pass along the local variable of the associated post above it. 由于您要通过其他部分呈现评论的部分表格,因此您需要传递其上方相关帖子的局部变量。

"comments/form", :locals => { :micropost => micropost } %> “评论/表格”,:locals => {:micropost => micropost}%>

and your form to something like this 和你的形式像这样

 <div class="CommentField"> <%= form_for ([micropost, @comment]) do |f| %> <%= f.text_area :content, :class => "CommentText", :placeholder => "Write a Comment..." %> <div class="CommentButtonContainer"> <%= f.submit "Comment", :class => "CommentButton b1" %> </div> <% end %> </div> 

In all my rails apps, that is all the association I would need to do for it to properly assign the Ids by itself. 在我所有的Rails应用程序中,这就是我需要做的所有关联,以便它自己正确分配ID。 I'm sure that will fix the issue. 我确定这可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM