【问题标题】:Using thumbs_up gem while iterating迭代时使用 thumbs_up gem
【发布时间】:2013-09-05 20:35:51
【问题描述】:

我在我的应用程序中使用 thumbs_up gem,我正在尝试找出在迭代时在控制器中保存投票的最佳方法。

这是我的模型:

class Vendor < ActiveRecord::Base
    has_many    :inventory_items
    has_many    :items, through: :inventory_items
    has_many    :shopping_lists, through: :inventory_items

    acts_as_voteable
end

我当前的控制器:

def vote_for_vendor
    # not sure what to put in here
end

def vote_against_vendor
    # not sure what to put in here
end

我目前的看法:

<% provide(:title, 'Stores') %>

<table class="table table-condensed table-hover">

<tr>
    <th>Name</th>
    <th>Address</th>
    <th>Favorite?</th>
</tr>

    <% @vendors.each do |v| %>
<tr>

    <td><%= v.name %></td>
    <td><%= v.address %></td>
    <td>
        <% if current_user.voted_for(v) %>
            <%= link_to 'unlike', vote_against_vendor_vendors_path(vendor_id: v.id), :remote => true %>
        <% else %>
            <%= link_to 'like', vote_for_vendor_vendors_path(vendor_id: v.id), :remote => true %>
        <% end %>
    </td>
</tr>

</table>

我见过的大多数示例都使用params([]) 将相关信息传递给控制器​​。我真的没有参数,因为这只是显示所有供应商的索引页。如何在迭代时使用此 gem 保存选票?提前致谢!

在 MrYoshi 的帮助下更新了控制器

class VendorsController < ApplicationController

    def index
        @vendors = Vendor.all
    end

    def vote_for_vendor
        vendor = Vendor.find(params[:vendor_id])
        current_user.vote_for(vendor)

        respond_to do |format|
            format.js
        end
    end

    def vote_against_vendor
        vendor = Vendor.find(params[:vendor_id])
        current_user.vote_against(vendor)

        respond_to do |format|
            format.js
        end
    end
end

我的路线:

resources :vendors do
    collection { post :vote_for_vendor }
    collection { post :vote_agaist_vendor }
  end

当前服务器错误

于 2013-09-06 10:07:29 -0700 开始 GET "/vendors/vote_for_vendor?vendor_id=4" for 127.0.0.1

AbstractController::ActionNotFound(找不到 VendorsController 的动作“显示”):

...........

在救援/布局(0.5毫秒)

【问题讨论】:

    标签: ruby-on-rails ajax erb


    【解决方案1】:

    我给你你想要的开始,剩下的我想你可以自己做:

    查看:

    <% if current_user.voted_for(v) %>
      <%= link_to 'unlike', vote_against_vendor_vendors_path(vendor_id: v.id), :remote => true %>
    <% else %>
      <%= link_to 'like', vote_for_vendor_vendors_path(vendor_id: v.id), :remote => true %>
    <% end %>
    

    控制器:

    def vote_for_vendor
      vendor = Vendor.find(params[:vendor_id])
      current_user.vote_for(vendor)
    
      render :nothing => true
    end
    

    既然你在上面有这个,那 vote_against 就很容易猜了;)

    【讨论】:

    • 啊酷,我知道它是如何工作的。好的。我现在正在渲染页面,但我的投票没有提交。我已经更新了我的控制器以反映更改。我必须为这些路线添加集合才能使它们正常工作。我也添加了这些,因为我不确定它们是否设置正确。另外,我添加了 js 和 ajax 的格式块。不知道为什么我的投票没有提交,想法?
    • @settheline 引发的错误是什么? 404 未找到?没有路线匹配 [url]?什么都没有?
    • 请求发送了吗?您的服务器是否收到请求?当它收到请求时会发生什么?
    • 检查出来,请求正在发送到服务器。我已经用服务器错误更新了问题。
    • 您需要在操作末尾添加render :nothing =&gt; true 以阻止服务器重定向/呈现任何其他操作。
    【解决方案2】:

    对于您的“当前服务器错误”(AbstractController::ActionNotFound (The action 'show' could not be found for VendorsController):),您需要将成员路由添加到您的config/routes.rb 文件中,如下所示:

    resources :vendors do
      member do
        post 'vote_for_vendor'
        post 'vote_against_vendor'
      end
    end
    

    您目前正在使用收集路由,这些路由适用于不需要特定 ID 即可工作的资源。我强烈推荐阅读 Rails Guide 的路线:

    Rails Guide Documentation on Routes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多