【问题标题】:How to add comments to statuses (like facebook)如何向状态添加评论(如 facebook)
【发布时间】:2016-08-24 16:58:38
【问题描述】:

我正在尝试改进我在树屋上所做的练习,其想法是重新制作一个小版本的 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

models/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/cmets/_form.html.erb我在下面的索引中创建了一个渲染:

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

查看/statuses/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>

现在是 cmets 部分:

model/comment.rb

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

控制器/cmets_controller.rb 类 CommentsController

  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

【问题讨论】:

  • 在索引中循环遍历@statuses 时,每个status 都有自己的cmets。因此,不是在 every 状态下渲染 all cmets,而是仅显示属于该状态的 cmets:status.comments.each do |comment|
  • 究竟是什么不起作用?
  • status.cmets.each 做 |comment|它说undefined local variable or method status' 代表#
  • 你必须在 in 你的@statuses 循环中使用它
  • 它已经在循环中了? @statuses 在文件末尾?

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


【解决方案1】:

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

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

【讨论】:

  • 感谢 :) 尚未修复,但我会一步一步搞定的 :)
  • 虽然这段代码可能有助于解决问题,但它并没有解释为什么和/或如何回答问题。提供这种额外的背景将显着提高其长期教育价值。请edit您的答案添加解释,包括适用的限制和假设。
猜你喜欢
  • 1970-01-01
  • 2013-11-24
  • 2014-10-03
  • 2012-01-26
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 2019-10-16
  • 1970-01-01
相关资源
最近更新 更多