评论部分未显示

时间:2015-01-04 23:19:42

标签: ruby-on-rails

我正在处理我的第一个rails应用程序,我正在尝试添加评论部分。评论将归入每个主题。我看到正在生成的评论,但我无法对其进行配置,以便我可以输入自己的评论。我将在下面列出我按照我的任务指示对代码所做的更改。

继续更改我的app / models / comment.rb

class Comment < ActiveRecord::Base
  belongs_to :post
  belongs_to :user

  validates :body, length: { minimum: 5 }
  validates :body, presence: true
  validates :user_id, presence: true
 end

继承我的app / models / user.rb

  class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  has_many :posts
  has_many :comments

  mount_uploader :avatar, AvatarUploader

   def admin?
   role == 'admin'
 end

 def moderator?
   role == 'moderator'
end
end

我还要更新db / seeds.rb

# Create Comments
100.times do
  Comment.create!(
     # user: users.sample,   # we have not yet associated Users with Comments
    post: posts.sample,
    user: users.sample,
    body: Faker::Lorem.paragraph
  )
end

更新了我的config / routes.rb

resources :comments, only: [:create]

更新了我的app / controllers / comments_controller.rb

class CommentsController < ApplicationController

    def create
@post = Post.find(params[:post_id])
@comment = current_user.comments.new(comment_params)
@comment.post = @post
@new_comment = Comment.new

authorize @comment
    end
end

我做的最后调整是app / views / comments / _form.html.erb

<% if current_user %>
<h4>Add a comment:</h4>
<%= form_for [@post, @post.comments.build], remote: true do |f| %>
<%= f.label :body %>
<%= f.text_field :body %>
<%= f.submit %>
<% end %>
<% end %>

我希望这对于我可能遗漏的某些类型的回应是足够的。我没有收到错误,我正在寻找创建评论的框不会出现。这是我想要完成的一个例子:

http://oi62.tinypic.com/f35h5y.jpg

编辑:

以下是app/views/posts/show.html.erb

<h1><%= markdown @post.title %></h1>

<div class="row">
  <div class="col-md-8">
         <small>
       <%= image_tag(@post.user.avatar.tiny.url) if @post.user.avatar? %>
       submitted <%= time_ago_in_words(@post.created_at) %> ago by
       <%= @post.user.name %>
   </small>
    <p><%= markdown @post.body %></p>
    <p><%= image_tag(@post.image_url) if @post.image? %></p>
  </div>
  <div class="col-md-4">
    <% if policy(@post).edit? %>
      <%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %>
    <% end %>
  </div>
  </div>

0 个答案:

没有答案