0

我有一个 Rails 应用程序,用户可以在其中上传 pin,我想使用[acts_as_commentable_gem][1]允许用户评论 pin,这是我的配置:

应用程序/模型/pin.rb

class Pin < ActiveRecord::Base
    acts_as_commentable 
end

应用程序/控制器/pins_controller.rb

def show
   @pin.find params[:id]
   @comment = @pin.comments.new
end

app/views/pins/show.html.erb

<%= form_tag "/pins/add_new_comment" do %>
    <%= hidden_field_tag "id", post.id %>
    <%= text_area_tag "comment[comment]" %>
    <%= submit_tag "Pin Comment" %>
    <% end %>

应用程序/模型/comment.rb

class Comment < ActiveRecord::Base

  include ActsAsCommentable::Comment

  belongs_to :commentable, :polymorphic => true

  default_scope -> { order('created_at ASC') }

  # NOTE: install the acts_as_votable plugin if you
  # want user to vote on the quality of comments.
  #acts_as_voteable

  # NOTE: Comments belong to a user
  belongs_to :user
end

应用程序/控制器/pin_controller.rb

def add_new_comment
    pin = Pin.find(params[:id])
    pin.comments << Pin.new(params[:comment])
    redirect_to :action => :show, :id => pin
end

最后在我的配置/路线中

get "/pins/add_new_comment" => "pins#add_new_comment", :as => "add_new_comment_to_pins", :via => [:pin]

但是我遇到了路由错误:

PinsController:Class 的未定义局部变量或方法“acts_as_commentable”

我真的不确定这个错误来自哪里,有什么想法吗?

4

1 回答 1

0

我不太确定,但你的路线不应该是这样的

get "/pins/:id/add_new_comment" => "pins#add_new_comment", :as => "add_new_comment_to_pins"
于 2014-08-25T10:20:34.227 回答