1
class CommentsController < ApplicationController
def create
@commentable= context_object()
@comment = @commentable.comments.build(params[:comment].merge(:user_id => current_user.id))
if @comment.save
respond_to do |format|
format.js
end
else
render :action => 'new'
end
end
private
def context_object
params[:constraint][:context_type].singularize.classify.constantize.find(context_id)
end
def context_id
params["#{ params[:constraint][:context_type].singularize }_id"]
end
end
此評論模塊爲我提供了很好的服務,但今天早上我碰到了一個麻煩,可能是因爲我使用了嵌套資源。從本質上講,我現在有一個網址,如:Rails 3 - 幫助評論模塊
/projects/3/albums/6/attachments/84
當我在該網頁上發表評論,我得到的錯誤:
ActiveRecord::RecordNotFound (Couldn't find Project without an ID):
app/controllers/comments_controller.rb:102:in `context_object'
app/controllers/comments_controller.rb:14:in `create'
我的路線文件看起來像:
resources :projects do
resources : albums do
resources :attachments
end
end
resources :attachments do
resources :comments, :only => [:create, :update,:destroy],
:constraint => {:context_type => "conversations"}
end
任何想法關於如何讓評論模塊可以很好地與project>Album>Attachment
發表評論?
感謝您的輸入,
我編輯了你的帖子以獲得可讀性。您能否澄清一下爲什麼您將資源評論嵌套在項目之外的附件>專輯層次結構中?你需要GET /附件並對它們發表評論嗎?你有沒有嘗試過資源評論下面的附件項目>相冊>附件? – tomeduarte 2011-01-26 00:14:12