Make_flaggable - 参数数量错误

时间:2014-07-11 07:29:15

标签: ruby-on-rails rubygems arguments sql-like helper

我正在尝试在我的Rails应用程序的教程部分中使用gem作为like /不同按钮( https://github.com/medihack/make_flaggable)但我发生了一个错误,它让我发疯。错误为 wrong number of arguments (2 for 1) ,并在视图调用方法toggle_like_button时发生。

在教程帮助器中有一个名为toggle_like_button的方法,它表示当前用户是否喜欢该视频。然后在教程控制器中我创建了一个方法def,它允许不同于以前喜欢的内容,反之亦然。

这是宝石:

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

这是application.rb

config.active_record.whitelist_attributes = false

这是教程助手中的toggle_like_button方法:

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

这是教程控制器:

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

这是调用tutorials_helper方法的视图:

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

1 个答案:

答案 0 :(得分:1)

假设你可以在旗帜的基础上喜欢和不喜欢教程。 试试这个:

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

另见:

使用您的宝石

# 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)