Ruby onRailsWebサイトにコメントシステムを実装しようとしました。私は基本的にこのスレッドから次のガイドラインに従いました:ユーザーページへのMicropostのコメント(Ruby on Rails)
ただし、投稿してもコメントが表示されず、すべてのコメントボックスの上部に「アセット」というテキストが表示されます。これはどこから来たのですか?
コードで更新:上記のリンクに示されているようにコメントを機能させるために3つのモデルを使用しています
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| %>
ルート.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>