所以我试图在我创建的图像上显示我在数据库的注释表中创建的注释。目的是创建一个像Facebook标记系统的应用程序。
我是Ruby新手,所以我不确定我需要向您展示什么,但这是图片的代码show.html.erb
<p id="notice"><%= notice %></p>
<div id="container">
<p>
<b>Title:</b>
<%= @image.title %>
</p>
<p>
<b>Filename:</b>
<%= @image.filename %>
<%= image_tag @image.filename %>
</p>
<p>
<b>Likes:</b>
<span id="images_<$=@image.id%>_likes_count"><%= @image.likes %></span>
</p>
<div id="comments">
<h2>Comments</h2>
<%= render :partial => "shared/comment", :collection => @image.comments%>
</div>
</div>
<%= link_to 'Like', like_image_path(@image), :method => :post %> |
<%= link_to 'Edit', edit_image_path(@image) %> |
<%= link_to 'Back', images_path %>
这是共享区域中评论的部分内容:
<div class="comment">
<p>
<span class="commentator"><%= comment.commentator.display_name %>say's</span>
<%= comment.comment %>
</p>
</div>
目标是shared/_comment.html.erb
使用方法display_name。对象comment.commentator将是一个朋友的实例,需要修改这个方法,这将是他们的名字和姓氏连接在一起。
我正在运行http://localhost:3000/artists/2
时收到此错误“私有方法`评论'呼吁nil:NilClass”
这是图像控制器中的代码
class ImagesController < ApplicationController
# GET /images
# GET /images.xml
def index
@images = Image.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @images }
end
end
# GET /images/1
# GET /images/1.xml
def show
@image = Image.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @image }
end
end
# GET /images/new
# GET /images/new.xml
def new
@image = Image.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @image }
end
end
# GET /images/1/edit
def edit
@image = Image.find(params[:id])
end
# POST /images
# POST /images.xml
def create
@image = Image.new(params[:image])
respond_to do |format|
if @image.save
format.html { redirect_to(@image, :notice => 'Image was successfully created.') }
format.xml { render :xml => @image, :status => :created, :location => @image }
else
format.html { render :action => "new" }
format.xml { render :xml => @image.errors, :status => :unprocessable_entity }
end
end
end
# PUT /images/1
# PUT /images/1.xml
def update
@image = Image.find(params[:id])
respond_to do |format|
if @image.update_attributes(params[:image])
format.html { redirect_to(@image, :notice => 'Image was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @image.errors, :status => :unprocessable_entity }
end
end
end
def like
@image = Image.find(params[:id])
@image.update_attribute :likes, @image.likes+1
respond_to do |format|
if request.xhr?
format.json {render :json => { :id => @image.id, :likes => @image.likes }, :layout => nil
}
else
format.html {redirect_to :back }
end
end
end
# DELETE /images/1
# DELETE /images/1.xml
def destroy
@image = Image.find(params[:id])
@image.destroy
respond_to do |format|
format.html { redirect_to(images_url) }
format.xml { head :ok }
end
end
end
def comments
@comment = Comment.find(params[:id])
end
这是图像模型
class Image < ActiveRecord::Base
has_many :publishings
has_many :artists, :through => :publishings
has_many :comments, :as => :resource, :class_name => "Commentable"
end
这是评论表
class CreateCommentables < ActiveRecord::Migration
def self.up
create_table :commentables do |t|
t.integer :commentator_id
t.integer :resource_id
t.string :resource_type
t.integer :commentator_id
t.string :commentator_type
t.text :comment
t.timestamps
end
end
def self.down
drop_table :commentables
end
end