【问题标题】:Clarification on how to use "thumbs_up" voting gem with Rails 3关于如何在 Rails 3 中使用“thumbs_up”投票 gem 的说明
【发布时间】:2011-06-21 22:05:02
【问题描述】:

我正在尝试在 Rails 3 应用程序上实现 thumbs_up 投票 gem,但实际实现中的说明尚不清楚。在需要 gem [gem 'thumbs_up'] 并创建并运行适当的迁移 [rails generate thumbs_up && rake db:migrate] 之后,自述文件解释如下:

要为模特投票,您可以执行以下操作:
*简写语法
voter.vote_for(voteable) # 添加+1 投票
voter.vote_against(voteable) # 添加 -1 票
voter.vote(voteable, vote) # 添加 +1 或 -1 投票: 投票 => 真 (+1),投票 => 假 (-1)

voter.vote_exclusively_for(voteable) # 删除任何以前的投票 特定的选民,并投票。
voter.vote_exclusively_against(voteable) # 删除任何以前的投票 特定的选民,并投反对票。*

我一直假设 README 示例中使用的“voter”和“voteable”是应用程序中对象的替身,但对我来说,用法仍然很模糊。

我的视图、控制器和 routes.rb 文件应该是什么样子的文字示例将是一个巨大的帮助。我花了几天时间试图弄清楚这一点!

在我的应用程序中,我有对帖子投票的用户 - 其中有两种类型 - 事件链接。使用 @posts %> 调用帖子,每个单独的帖子都使用“_event.html.erb”或“ _link.html.erb" - 取决于是事件还是链接。

【问题讨论】:

    标签: ruby-on-rails-3 vote-up-buttons


    【解决方案1】:

    路由错误

    没有路线匹配 {:action=>"vote_up", :controller=>"microposts", :id=>无}

    这是我正在使用的链接,并假设这是未正确指定路由的地方。我跑了 rake 路线,有一条路线叫做 vote_up_micropost。还有什么我应该调查的。谢谢

    这是我添加的链接

    <%= link_to('vote for this post!',
        vote_up_micropost_path(@microposts),
        :method => :post) %>
    

    【讨论】:

    • 您的 @microposts 变量可能为零...这就是它试图将您发送到 :id => nil 的原因。
    • 在哪里以及如何设置@microposts 变量?
    【解决方案2】:

    这只是布雷迪回答的延续。

    Brady 在他看来有以下代码

    <%= link_to('vote for this post!', vote_up_post_path(@post), :method => :post) %>
    

    他的意思是.. 因为 link_to 默认使用 :method =&gt; 'get' 并且他想使用 post 更新记录而不是 get 所以他使用 :method =&gt; 'post'

    你可以使用 ,因为按钮默认使用:method =&gt; :post

    所以路线应该是

    resources :posts do
      member do
        post :vote_up
      end
    end
    

    post :vote_up 成员内部,它是method =&gt; :post 而不是后控制器

    但是如果你决定使用link_to 而不使用:method =&gt; :post 这样的东西

    <%= link_to('vote for this post!', vote_up_post_path(@post)) %>
    

    那么你的路由应该是

    resources :posts do
       member do
          get :vote_up
       end
    end
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      希望能帮到你。

      生成器应该已经为您创建了一个投票模型。这是持有所有选票的模型,但您可以通过上述方法间接与之交互。

      所以,对你来说:

      class User < ActiveRecord::Base
        acts_as_voter
      end
      
      class Post < ActiveRecord::Base
        acts_as_voteable
      end
      

      这将使您在每个模型中设置 thumbs_up 方法。

      然后,例如,如果 PostsController 中有一个控制器操作链接到您网站上的“向上箭头”,您可以为该用户创建对该帖子的投票。

      这样的视图:

      <%= link_to('vote for this post!', vote_up_post_path(@post), :method => :post) %>
      

      还有一个这样的 routes.rb:

      resources :posts do
        member do
          post :vote_up
        end
      end
      

      最后,在控制器中:

      class PostsController < ApplicationController
        def vote_up
          begin
            current_user.vote_for(@post = Post.find(params[:id]))
            render :nothing => true, :status => 200
          rescue ActiveRecord::RecordInvalid
            render :nothing => true, :status => 404
          end
        end
      end
      

      【讨论】:

      • 嘿 brady8,acts_as_voter 的型号不止一个吗?例如,假设我有一个 User 模型和一个 client 模型。他们俩都可以作为选民,它可以正常工作吗? span>
      • @brady8 我得到了与下面的答案中提到的相同的 NoMethod 错误。你能详细说明一下吗?
      • 这只是将我带到 /posts/:id/vote_up 的一个空模板
      • @ajbraus,是的,这就是它的编码方式(不渲染)。投票发生了,这才是最重要的……你之后做什么取决于你和特定的应用程序。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      相关资源
      最近更新 更多