Search code examples
ruby-on-railsrubygemsargumentssql-likehelper

Make_flaggable - wrong number of arguments


I'm trying to use the gem for the like/unlike button in a tutorial section of my Rails application ( https://github.com/medihack/make_flaggable ) but I occurred in one error which it's driving me crazy. The error is wrong number of arguments (2 for 1) and occurs when the view calls the method toggle_like_button.

In the tutorials helper there's a method called toggle_like_button that says if the current user likes or don't like the video yet. Then in the tutorials controller I've created a method def like that allows to unlike a content previously liked and vice versa.

This is the gem:

gem 'make_flaggable', :git => 'https://github.com/medihack/make_flaggable.git'

This is in the application.rb:

config.active_record.whitelist_attributes = false

This is the toggle_like_button method in tutorials helper:

def toggle_like_button (tutorial, user)
    if user.flagged?(user, :like)
      link_to 'Unlike', like_tutorial_path(tutorial)
    else
      link_to 'Like', like_tutorial_path(tutorial)
    end
  end

This is the tutorials controller:

def like
    @tutorial = Tutorial.find(params[:id])
    if current_user.flagged?(@tutorial, :like)
       current_user.unflag(@tutorial, :like)
      msg = 'you now like'
    else
      current_user.flag(@tutorial, :like)
      msg = 'you dislike'
    end
    redirect_to tutorial_path, :notice => msg
  end

This is the view which calls the tutorials_helper method:

<% if @current_user.present? %>
    <%= toggle_like_button(@tutorial, @current_user) %>
    <% end %>

Solution

  • Assuming you can like and dislike tutorial on the basis of flag. Try this:

    def toggle_like_button (tutorial, user)
        if user.flagged?(tutorial, :like)
          link_to 'Unlike', like_tutorial_path(tutorial)
        else
          link_to 'Like', like_tutorial_path(tutorial)
        end
    end
    

    Also see:

    usage of your gem

    # Returns true if the flagger flagged the flaggable, false otherwise.
    user.flagged?(article, :flag_name)
    
    # Returns true if the flagger flagged the flaggable, false otherwise.
    user.flagged?(article)