0
我试图在我的Ruby on Rails网站上实现一个评论系统。 我基本上遵循这个线程的这些准则:Micropost's comments on users page (Ruby on Rails)当您的RoR网站显示文本“资产”时,这意味着什么?
但是,评论不显示当我发布和文本“资产”显示在每个评论框的顶部。这是从哪里来的?
与代码更新时间: 我使用的三种模式,试图获得工作的意见如在上面的链接
user.rb
class User < ActiveRecord::Base
has_many :microposts, dependent: :destroy
has_many :comments
micropost.rb
class Micropost < ActiveRecord::Base
attr_accessible :content, :image, :comment_content
belongs_to :user
has_many :comments, dependent: :destroy
validates :user_id, presence: true
validates :content, presence: true
default_scope order: 'microposts.created_at DESC'
def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
user_id: user.id)
end
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
条
评论控制器
class CommentsController < ApplicationController
def create
@micropost = Micropost.find(params[:micropost_id])
@comment = Comment.new(params[:comment])
@comment.micropost = @micropost
@comment.user = current_user
if @comment.save
redirect_to(:back)
else
render 'shared/_comment_form'
end
end
end
控制器微柱
class MicropostsController < ApplicationController
before_filter :signed_in_user
before_filter :correct_user, only: :destroy
def create
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Posted"
redirect_to root_path
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
redirect_to root_path
end
private
def correct_user
@micropost = current_user.microposts.find_by_id(params[:id])
redirect_to root_path if @micropost.nil?
end
end
用户控制器
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
@comment = Comment.new
end
评论形式
<%= form_for([micropost, @comment]) do |f| %>
routes.rb中
resources :microposts do
resources :comments
end
鉴于微柱
<li>
<span class="content"><%= simple_format(micropost.content) %></span>
<%= image_tag micropost.image_url(:thumb).to_s %><br>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<%= render 'shared/comment_form', micropost: micropost %>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %><br>
<% end %>
</li>
你认为这是与图像的东西?我发布了最重要的微博视图 – 2013-03-10 08:59:20
如果我理解你正确的话,你说你的表单中有一个文本“资产”。所以它呈现在某个地方。检查你的'shared/comment_form',也许有一个占位符文本。 OR:它是图像的alt标记,图像不可用,因此alt文本被渲染。 – YvesR 2013-03-10 09:08:10
是的,那就是我所问的。但是,我没有任何地方可以在任何地方使用“资产”文本。使用我的鼠标光标在显示的页面上甚至无法突出显示文字 – 2013-03-10 09:31:06