1

アプリケーションで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([])、関連情報をコントローラーに渡すために使用されていました。これはすべてのベンダーを表示するインデックス ページにすぎないため、実際にはパラメーターはありません。反復中にこの宝石を使用して投票を保存するにはどうすればよいですか? 前もって感謝します!

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 に 127.0.0.1 の GET "/vendors/vote_for_vendor?vendor_id=4" を開始しました

AbstractController::ActionNotFound (VendorsController のアクション「表示」が見つかりませんでした):

…………

/Users/#Myname/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/unknown_action.erb をレスキュー/レイアウト内でレンダリング (0.5ms)

4

2 に答える 2

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 は非常に簡単に推測できます ;)

于 2013-09-05T20:46:16.047 に答える
0

「現在のサーバー エラー」( ) については、次のようにメンバー ルートをファイル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 ガイドを読むことを強くお勧めします。

ルートに関する Rails ガイド ドキュメント

于 2013-09-08T14:54:13.233 に答える