Micropost对用户页面的评论(Ruby on Rails)

时间:2012-03-12 14:44:10

标签: ruby-on-rails ruby comments

在用户页面上我有很多微博,我想在每个微博上添加评论表和评论。

我有三种型号:User,Micropost,Comment。

user.rb

class User < ActiveRecord::Base
  has_many :microposts, dependent: :destroy
  has_many :comments
end

micropost.rb

class Micropost < ActiveRecord::Base
  belongs_to :user
  has_many :comments, dependent: :destroy
end

comment.rb

class Comment < ActiveRecord::Base
  attr_accessible :comment_content

  belongs_to :user
  belongs_to :micropost

  validates :comment_content, presence: true
  validates :user_id, presence: true
  validates :micropost_id, presence: true  
end

comments_controller.rb

class CommentsController < ApplicationController

  def create
    @comment = current_user.comments.build(params[:comment])
    if @comment.save
       flash[:success] = "Comment created!"
       redirect_to current_user
    else
      render 'shared/_comment_form'
    end
  end
end

_micropost.html.erb

<tr>
  <td class="micropost">
    <span class="content"><%= wrap(micropost.content) %></span>
    <span class="timestamp">
      Posted <%= time_ago_in_words(micropost.created_at) %> ago.    
    </span>
    <%= render 'shared/comment_form' %>
   </td>
</tr>

评论表单

<%= form_for(@comment) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :comment_content %>
  </div>
  <button class="btn" type="submit">
    Create
  </button>
<% end %>

每个微博都必须有自己的评论。 在我的数据库中,我有

的评论表
  

id / comment_content / user_id / micropost_id

列。

评论没有创建,因为RoR无法理解哪个微博属于这个新评论。 如何在我的数据库中获取所有需要的信息?

更新

users_controller

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
    @comment = Comment.new
  end

microposts_controller

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Micropost created!"
      redirect_to current_user
    else
      render 'shared/_micropost_form'
    end
  end

解!!!

非常感谢carlosramireziii和Jon!他们都是对的

comments_controller

  def create
    @micropost = Micropost.find(params[:micropost_id])
    @comment = Comment.new(params[:comment])
    @comment.micropost = @micropost
    @comment.user = current_user
    if @comment.save
       flash[:success] = "Comment created!"
       redirect_to current_user
    else
      render 'shared/_comment_form'
    end
  end

_micropost.html.erb

<%= render 'shared/comment_form', micropost: micropost %>

评论表单

<%= form_for([micropost, @comment]) do |f| %>

的routes.rb

resources :microposts do
  resources :comments
end

2 个答案:

答案 0 :(得分:0)

我会在routes.rb文件中使用嵌套资源进行微博和类似的评论:

resources :microposts do
  resources :comments
end

然后在您通过micropost_comments_path(@micropost)网址助手访问的评论控制器中,您可以执行以下操作以在创建操作中构建关联:

def create
  @micropost = Micropost.find(params[:micropost_id])
  @comment = Comment.new(params[:comment])
  @comment.micropost = @micropost
  @comment.user = current_user

  if @comment.save
    ...
end

您可以使用merge方法减少代码行数,但我发现这有时会降低代码的可读性,因为如果在验证错误后重新显示表单,则需要@micropost对象,因为好吧,只需获取记录。

答案 1 :(得分:0)

尝试将当前微博传递给评论部分

<%= render 'shared/comment_form', micropost: micropost %>

然后将微博添加到评论form_for电话

<%= form_for([micropost, @comment]) do |f| %>

确保您的路线嵌套

# in routes.rb
resources :microposts do
  resources :comments
end

然后在CommentsController

中建立微博的评论
def create
  @micropost = Micropost.find(params[:micropost_id])
  @comment = @micropost.comments.build(params[:comment])
  ...
end