我有一个用户可以上传图钉的rails应用程序,我想使用[acts_as_commentable_gem][1]
允许用户注释图钉,这是我的配置:
应用/模型/ pin.rb
class Pin < ActiveRecord::Base
acts_as_commentable
end
应用/ controlers / pins_controller.rb
def show
@pin.find params[:id]
@comment = @pin.comments.new
end
应用/视图/针/ 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 %>
app / models / 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]
但是我遇到了路由错误:
未定义的局部变量或方法`acts_as_commentable&#39; for PinsController:Class
我真的不确定这个错误来自哪里,有什么想法?
答案 0 :(得分:0)
我不太确定,但你的路线不应该像
get "/pins/:id/add_new_comment" => "pins#add_new_comment", :as => "add_new_comment_to_pins"