jackdempsey / acts_as_commentable

The ActiveRecord acts_as_commentable plugin
http://juixe.com/svn/acts_as_commentable/
MIT License
834 stars 178 forks source link

ActiveRecord::AssociationTypeMismatch #37

Closed napster3000 closed 10 years ago

napster3000 commented 10 years ago

May you write an full example in the Readme file?

schermata 2013-09-02 alle 22 33 50

schermata 2013-09-02 alle 22 33 31

schermata 2013-09-02 alle 22 34 56

jackdempsey commented 10 years ago

hey napster,

Nope, doesn't look right. If you want to push an example app somewhere, I can try to take a look at it, but i'm extremely slammed with stuff right now so no guarantee on how quickly I can get back. FWIW, I generally do comments in their own top level form, not as a nested model.

On Mon, Sep 2, 2013 at 1:52 AM, napster3000 notifications@github.comwrote:

I get this error. Is that right? May you write an example in Readme?

[image: schermata 2013-09-02 alle 10 50 31]https://f.cloud.github.com/assets/539132/1066152/c4159c58-13ac-11e3-9f96-154969943ed5.png

[image: schermata 2013-09-02 alle 10 50 01]https://f.cloud.github.com/assets/539132/1066151/b0efbf0a-13ac-11e3-91e3-354e850aa1cc.png

[image: schermata 2013-09-02 alle 10 48 24]https://f.cloud.github.com/assets/539132/1066145/97e33ea6-13ac-11e3-9735-c38811c6b2cf.png

— Reply to this email directly or view it on GitHubhttps://github.com/jackdempsey/acts_as_commentable/issues/37 .

napster3000 commented 10 years ago

Ok, I understand. But If I have to comment to a "status" wrote by a user, the only way is a nested model, isn't it? Do you have an "example app" which use act_as_commentable for Rails 4?

jackdempsey commented 10 years ago

But If I have to comment to a "status" wrote by a user, the only way is a nested model, isn't it?

Nope, you can post to a CommentsController with appropriate relationships in the params and it will create the comment just fine. Every app is different but I wouldn't be surprised if I saw something like:

1 class CommentsController < ApplicationController 2 def create 3 comment = Comment.new comment_params 4 commentable = params[:comment][:commentable_type].constantize.find(params[:comment][:commentable_id]) 5 comment.commentable = commentable 6 comment.creator = current_user 7 if comment.save 8 flash[:success] = "Comment saved." … 14 private 15 16 def comment_params 17 params.require(:comment).permit(:comment) 18 end

Anytime you take something from params and do an eval'ish thing on it (like constantize) you want to be careful you're not exposing anything bad.

Do you have an "example app" which use act_as_commentable for Rails 4

No example app but the above code does use the strong_params style.

hope that helps!

— Reply to this email directly or view it on GitHubhttps://github.com/jackdempsey/acts_as_commentable/issues/37#issuecomment-23678688 .