如何向状态添加评论(如facebook)

时间:2016-08-23 00:46:49

标签: ruby-on-rails ruby ruby-on-rails-4

我正在尝试改进我在树屋上所做的练习,我的想法是重新制作一个小版本的facebook,用户可以发布状态。

现在我希望用户可以评论任何状态......我有点失落...... 我们的想法是让所有人都在同一页面上(如果可能的话,就像在真正的Facebook上一样) 所以评论表和"显示"内容...

我希望有人能帮助我:) This is my github repository

我想我不知道如何将变量从控制器调用到另一个...... 如果有人能用非常简单的话语来解释我...我不是母语为英语的人......所以有时候这很难......

以下是状态部分

控制器/ statuses_controller / RB

class StatusesController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update]
  before_action :set_status, only: [:show, :edit, :update, :destroy]

  def index
   @statuses = Status.all
   @comments = Comment.all
  end

 def show
  @status = Status.find(params[:id])
  @comments = @status.comments.all
 end

 def new
  @status = Status.new
  @comment = @status.comments.build
 end

def create
 @status = Status.new(status_params)
 @status.user = current_user
 respond_to do |format|
   if @status.save
     format.html { redirect_to @status, notice: 'Status was successfully created.' }
     format.json { render :show, status: :created, location: @status }
   else
     format.html { render :new }
     format.json { render json: @status.errors, status: :unprocessable_entity }
   end
 end
end

def update
 respond_to do |format|
  if @status.update(status_params)
    format.html { redirect_to @status, notice: 'Status was successfully updated.' }
    format.json { render :show, status: :ok, location: @status }
  else
    format.html { render :edit }
    format.json { render json: @status.errors, status: :unprocessable_entity }
  end
 end
end

def destroy
 @status.destroy
 respond_to do |format|
  format.html { redirect_to statuses_url, notice: 'Status was successfully destroyed.' }
  format.json { head :no_content }
 end
end

private
  def set_status
   @status = Status.find(params[:id])
 end

 def status_params
  params.require(:status).permit(:user_id, :content, :comments_attribute[:id, :status_id, :content])
 end
end

模型/ status.rb

class Status < ActiveRecord::Base
  belongs_to :user
  has_many :comments

  default_scope -> { order(created_at: :DESC)}

  validates :content, presence: true,
            length: {minimum: 2}
  validates :user_id, presence: true
end

views / comments / _form.html.erb 我在下面的索引中创建了一个渲染:

<% simple_form_for @status.comments do |f|%>
 <%= f.input :content %>
 <%= f.button :submit %>
<% end  %>

查看/状态/ index.html.erb

<div class="page-header">
  <h1>All of the Statuses</h1>
</div>
<%= link_to "Post A New Status", new_status_path, class: "btn btn-success"%>
<br>
<br>
<% @statuses.each do |status| %>

  <div class="status">

    <div class="row">
      <div class="col-xs-1 avatar">
        <%= image_tag status.user.avatar.thumb if status.user.avatar?%>
      </div>
      <div class="col-xs-7">
        <h4><%= status.user.full_name%></h4>
      </div>
    </div>

    <div class="row">
      <div class="col-xs-8">
       <p><%= simple_format(status.content) %></p>
      </div>
    </div>

    <div class="row">
     <div class="col-xs-8">
      <%= link_to time_ago_in_words(status.created_at) + " ago", status %>
        <% if status.user == current_user %>
          <span class="admin">
        |   <%= link_to "Edit", edit_status_path(status) %> |
            <%= link_to "Delete", status, method: :delete, data: {confirm: "Are you sure?"} %>
          </span>
        <% end  %>
       </div>
     </div>

    <div class="row">
      <div class="col-xs-12">
       <p>Comments</p>

       <% @comments.each do |comment| %>
         <%= comment.content %>
       <% end %>
     </div>
   </div>

   <div class="row">
    <div class="col-xs-12">
      <%= render "comments/form" %>
    </div>
   </div>

</div>
  

现在评论部分:

模型/ comment.rb

class Comment < ActiveRecord::Base
 belongs_to :status
 belongs_to :user
end

控制器/ comments_controller.rb       class CommentsController&lt; ApplicationController中

  def create
   @comment = Comment.new(params_comment)
  end

  def index
   @statuses = Status.all
   @comments = Comment.all
   @comment = Comment.find_by(params[:id])
  end

 private

 def params_comment
   params.require(:comment).permit(:content)
 end
end

的routes.rb

  resources :statuses do
   resources :comments
  end

user.rb 那是我在那里的一部分

  has_many :statuses
  has_many :comments

1 个答案:

答案 0 :(得分:0)

您的评论创建方法应如下所示:

@status = Status.find(params[:status_id])
@comment = @status.comments.create(comment_params)
@comment.user_id = current_user.id if current_user
@comment.save