不确定如何实施GEM' Thumbs_up' [rails 4]

时间:2014-08-22 11:53:23

标签: ruby-on-rails ruby ruby-on-rails-4

嗨,我对铁杆很新,任何帮助都会非常感激。我正在尝试实现thumbs_up GEM。我已经阅读了文档,但仍然发现完全实现它很有挑战性。

  1. 我想要的是当用户点击thumbs_up图像时显示增加的数字,反之亦然当用户点击thumbs_down图像时
  2. 我的意思是:如果用户点击图像thumbs_up你会看到数字为1,如果另一个用户点击图像thumbs_up,则数字增加到2 [显示2个用户喜欢该事件给它一个thumbs_up]

    任何有关解释的建议都会有所帮助 - 以下是我已达到的阶段

      

    我有一个。)运行g thumbs_up& b。)rake db:migrate

    型号:

    #Event.rb
    class Event < ActiveRecord::Base
      belongs_to :user
      acts_as_voteable
    end
    
    #User.rb
    class User < ActiveRecord::Base
      has_many :events, dependent: :destroy
      acts_as_voter
    end
    

    控制器

    class EventsController < ApplicationController
      before_action :set_event, only: [:show, :edit, :update, :destroy]
      before_filter :authenticate_user!
    
      def index
        @events = Event.order(:date)
      end
    
      def show
        @commentable = @event
        @comments = @commentable.comments
        @comment = Comment.new
      end
    
      def new
        @event = Event.new
      end
    
      def edit
      end
    
      def create
        @event = Event.new(event_params)
        @event.user = current_user
    
        respond_to do |format|
          if @event.save
            format.html { redirect_to @event, notice: 'Event was successfully created.' }
            format.json { render :show, status: :created, location: @event }
          else
            format.html { render :new }
            format.json { render json: @event.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def update
        respond_to do |format|
          if @event.update(event_params)
            format.html { redirect_to @event, notice: 'Event was successfully updated.' }
            format.json { render :show, status: :ok, location: @event }
          else
            format.html { render :edit }
            format.json { render json: @event.errors, status: :unprocessable_entity }
          end
        end
      end
    
      def destroy
        @event.destroy
        respond_to do |format|
          format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
          format.json { head :no_content }
        end
      end
    
      def vote_for
          @event = Event.find(params[:id])
          current_user.vote_for(@event)
          respond_to do |format|
            format.js
          end
      end
    
      private
        # Use callbacks to share common setup or constraints between actions.
        def set_event
          @event = Event.find(params[:id])
        end
    
        # Never trust parameters from the scary internet, only allow the white list through.
        def event_params
          params.require(:event).permit(:name, :description, :date, :time, :city, :price, :user_id)
        end
    end
    

    观看次数:app / views / events / show.html.erb

    <p>
      <strong>Name:</strong>
      <%= @event.name %>
    </p>
    
    <p>
      <strong>Description:</strong>
      <%= @event.description %>
    </p>
    
    <p>
      <%=link_to image_tag('thumbs_up', :border => 0), vote_for_event_path(@event), :remote => true %>
      <%=link_to image_tag('thumbs_down', :border => 0), vote_against_event_path(@event), :remote => true %>
    </p>
    

    路线:

    Rails.application.routes.draw do
    
      devise_for :users
    
      resources :events do 
        resources :comments, only: [:create, :destroy]
        member do
          post :vote_for, :vote_against
        end
      end
    end
    

1 个答案:

答案 0 :(得分:2)

要在您的视图中显示投票,请使用votes_for方法和votes_against方法。

例如,在您的视图中添加以下行:

<p>
  <strong>Votes For:</strong>
  <%= @event.votes_for %>
</p>

<p>
  <strong>Votes Against:</strong>
  <%= @event.votes_against %>
</p>

因为你正在学习Rails,我建议你尝试使用基本的HTML链接而不是AJAX链接:

<%=link_to image_tag('thumbs_up', :border => 0), vote_for_event_path(@event) %>
<%=link_to image_tag('thumbs_down', :border => 0), vote_against_event_path(@event) %>

基本链接可以帮助您了解正在发生的事情,您的控制器将重新加载整个页面。

当您获得基本链接时,请阅读有关AJAX的信息,以及如何使用Rails link_toremote: true来更新HTML div

有多种方法可以实现此目的:您可以阅读有关使用Rails响应程序,或使用coffescript,或使用jQuery附加链接处理程序的信息。

还有各种用户界面解决方案,例如因为您知道原始投票而立即更新投票,并且您可以立即在客户端增加投票。

这是一个相关的StackOverflow问题,其中包含一系列关于remote_to和替换div的良好答案和讨论:rails link_to :remote