RailsCasts Pro episodes are now free!

Learn more or hide this

climatecheck's Profile

GitHub User: climatecheck

Site: http://climate-check.com

Comments by

Avatar

Is there no in-DOM solution for establishing a target element to load the response? For example:

<%= link_to "Show panda", "images/panda.gif", {remote: true, data-target: '#panda-holder' %>

Then every remote link could call something generic like:

$('[data-remote=true]').on('click', function (e) {
e.preventdefault();
$($(this).attr('data-remote')).load($(this).attr('href'));
});

Or it could be more elegantly worked into rails.js. This avoids splitting ajax behaviour into dozens of .js.erb files, and writing format.js for every method in the controller. Sure wish something like that was built into rails.js.

Turbolinks is a great solution and I'm using it in a re-write, but for now I have to deal with a rails 3.1.10 app with loads of .on and e.preventdefaults in application.js

Avatar

That's far from getting actually hacked though and there's no loss of customer information.

Avatar

Do you know what the tax on SQL would be for this? I just used one reputation to count positive (votes up) and another to count negative (votes down).

Avatar

Figured it out. The most recent version (2.2) has the evaluators_for method which reports users which have votes on a resource with the "metric".

This is my vote method which now includes "un-voting":

def vote
  value = params[:type] == "up" ? 1 : -1
  @public_comment = PublicComment.find(params[:id])

  have_voted = @public_comment.evaluators_for(:pubcom_votes_up) << @public_comment.evaluators_for(:pubcom_votes_down)

  unless have_voted.include?(@current_user) # vote
    @public_comment.add_or_update_evaluation(:"pubcom_votes_#{params[:type]}", value, @current_user)
  else                                      # unvote
    @public_comment.delete_evaluation(:"pubcom_votes_#{params[:type]}", @current_user)
  end

  respond_to do |format|
    format.js # vote.js.erb
  end
end
Avatar

I've implemented "un-voting". This railscast is dated if you're using the latest version of activerecord-reputation-system. Forget the voted_for? method on the user model and use evaluators_for.

Here's my vote method:

def vote
  value = params[:type] == "up" ? 1 : -1
  @public_comment = PublicComment.find(params[:id])

  have_voted = @public_comment.evaluators_for(:pubcom_votes_up) << @public_comment.evaluators_for(:pubcom_votes_down)

  unless have_voted.include?(@current_user) # vote
    @public_comment.add_or_update_evaluation(:"pubcom_votes_#{params[:type]}", value, @current_user)
  else                                      # unvote
    @public_comment.delete_evaluation(:"pubcom_votes_#{params[:type]}", @current_user)
  end

  respond_to do |format|
    format.js # vote.js.erb
  end
end

I'm tracking up votes and downvotes with 2 parameters. If anyone knows a way to keep it to 1 parameter but show # of upvotes and # of downvotes, let me know!

Avatar

Any idea for how to adapt user.voted_for? for a specific metric? I'm tracking up votes and down votes separately for comments in my app and would like to display a "toggled" look if a user has already voted.

This is my vote method on my comments controller:

def vote
  value = params[:type] == "up" ? 1 : -1
  @public_comment = PublicComment.find(params[:id])
  if value > 0
    @public_comment.add_or_update_evaluation(:pubcom_votes_up, value, @current_user)
  else
    @public_comment.add_or_update_evaluation(:pubcom_votes_down, value, @current_user)
  end

  respond_to do |format|
    format.js # vote.js.erb
  end
end