wazery / ratyrate

:star: A Ruby Gem that wraps the functionality of jQuery Raty library, and provides optional IMDB style rating.
http://ratingmoviestore.herokuapp.com
237 stars 120 forks source link

Articles with comments but gems not working #79

Open Oppenheimer1 opened 9 years ago

Oppenheimer1 commented 9 years ago

I have my models as:

class Article < ActiveRecord::Base
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"   
  has_many :comments, dependent: :destroy
  validates :title, presence: true,
                    length: { minimum: 5 }
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

  ratyrate_rater
end

And

class Comment < ActiveRecord::Base
  belongs_to :article

  ratyrate_rateable "fun"
end

And in my view _comment.html.erb I have

<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>

<p>
  <strong>Comment:</strong>
  <%= comment.body %>
</p>

<p>
 Speed : <%= rating_for , "speed", :star => 10 %>
</p>

<p>
  <%= link_to 'Destroy Comment', [comment.article, comment],
               method: :delete,
               data: { confirm: 'Are you sure?' } %>
</p> 

But I'm getting an error message:

NoMethodError in Articles#show Showing app/views/comments/_comment.html.erb where line #12 raised:

undefined method `average' for nil:NilClass Extracted source (around line #12): 9 10 11 12 13 14 15

<%= rating_for @comment, "fun" %>

Please help.

wazery commented 9 years ago

@Oppenheimer1 you are specifying Speed : <%= rating_for , "speed", :star => 10 %> although "speed" dimension is not added to the line ratyrate_rateable "fun" in your Comment model, so please change it to ratyrate_rateable "fun", "speed".

Also notice that you didn't pass @comment to rating_for method. Speed : <%= rating_for , "speed", :star => 10 %>

Oppenheimer1 commented 9 years ago

I updated the model to have:

class Comment < ActiveRecord::Base
  belongs_to :article

  ratyrate_rateable "fun", "speed"
end

And the view _comment.html.erb to

<p>
  <strong>Commenter:</strong>
  <%= comment.commenter %>
</p>

<p>
  <strong>Comment:</strong>
  <%= comment.body %>
</p>

<p>
 Speed : <%= rating_for @comment, "fun", :star => 10 %>
</p>

<p>
  <%= link_to 'Destroy Comment', [comment.article, comment],
               method: :delete,
               data: { confirm: 'Are you sure?' } %>
</p>

But I'm getting the error:

Showing app/views/comments/_comment.html.erb where line #12 raised:

undefined method `average' for nil:NilClass Extracted source (around line #12):

9
10
11
12
13
14
15

   </p>

   <p>
    Speed : <%= rating_for @comment, "fun", :star => 10 %>
   </p>

   <p>
wazery commented 9 years ago

Could you use an upstream version of the gem, just change your Gemfile as follows:

gem 'ratyrate', github: 'wazery/ratyrate'

Then run: bundle update

Oppenheimer1 commented 9 years ago

I updated the gem and didn't change anything else but I'm getting the following error:

NoMethodError in Articles#show Showing app/views/comments/_comment.html.erb where line #12 raised:

undefined method `average' for nil:NilClass Extracted source (around line #12): 9 10 11 12 13 14 15

Speed : <%= rating_for @comment, "fun", :star => 10 %>

I have my models Articles and Comments with the commenter having the ability to comment on the Articles but not make edits to the Articles so I have my user model as Articles. I'm not sure What I'm doing wrong.

wazery commented 9 years ago

I don't understand how your user model is replaced by article, you need to make the entity that will do the comment have ratyrate_rater helper in its model, which is the user in most of the cases.

Oppenheimer1 commented 9 years ago

I don't have a user class. I have Articles which I want to be rated using the star rating and Comments in which a commenter can comment on each of the Articles. I think this is making it more complicated but I don't know how to reconfigured my models to work with the ratyrate gem.

wazery commented 9 years ago

Sorry for being late in replying, I got some sleep. You mentioned a "commenter" what is that entity actually in your models, there must be a user entity which will create new articles comments, your implementation is not clear for me.

I see in your code the following line <%= comment.commenter %> what is the commenter? I don't see any association related to it.

Also in your comment you say

"I have Articles which I want to be rated using the star rating"

So the article is what will be rated not the comment as you write in your code?

Please if you can give me some more code snippets or access to your source code it would be better.

Oppenheimer1 commented 9 years ago

I pushed the source code up to github:

https://github.com/Oppenheimer1/test1

wazery commented 9 years ago

I will open a PR against your app repo when everything is wired up.

Oppenheimer1 commented 9 years ago

Is there anything else I need to upload or is the source code ok.

wazery commented 9 years ago

It's ok.

wazery commented 9 years ago

I opened a PR, please check it.

Oppenheimer1 commented 9 years ago

Apologies I had uploaded the wrong version. The correct one is at:

https://github.com/Oppenheimer1/test_2 I made the change to the _comment.html.erb but I'm still getting an error. Sorry for the confusion.

veduket commented 9 years ago

I've edited your code and created a pull request. The problem was there was no current_user method available for rating_for to work with. Please see the changes in articles_controller.rb However it's easier to use Devise to create the authentication. You can find the pull request https://github.com/Oppenheimer1/test_2/pull/1