如何将喜欢或赞好的帖子显示为可食用的宝石

首先,我正在使用的宝石是https://github.com/ryanto/acts_as_votable

你好!我是Rails的新手,正在尝试构建一个学生可以喜欢公司发布的实习职位的应用程序。到目前为止,我已经完成了所有工作,但是我希望学生能够看到他们投票支持的公司的帖子。

到目前为止,这是我的实习控制器,是使用脚手架创建的,尚未包含全部。

class InternshipsController < ApplicationController
  before_action :set_internship,only: [:show,:edit,:update,:destroy]
  

  
  def upvote
    @internship = Internship.find(params[:id])
    @internship.liked_by current_student
    redirect_to internships_path
    #can change current_user to any other voter
  end

  def downvote
    @internship = Internship.find(params[:id])
    @internship.disliked_by current_student
    #can change current_user to any other voter
  end

  # GET /internships
  # GET /internships.json
  def index
    @internships = Internship.all
  end

  # GET /internships/1
  # GET /internships/1.json
  def show
  end

我的实习模式

class Internship < ApplicationRecord
     belongs_to :company
     acts_as_votable
end
我的学生模特

class Student < ApplicationRecord
  acts_as_voter
  # Include default devise modules. Others available are:
  # :confirmable,:lockable,:timeoutable,:trackable and :omniauthable
  devise :database_authenticatable,:registerable,:recoverable,:rememberable,:validatable
end

我的实习生/index.html.erb,我希望学生能够“投票”实习。

<% @internships.each do |internship| %>
      <tr>
        <td><%= internship.title %></td>
        <td><%= internship.requirements %></td>
        <td><%= internship.location %></td>
        <td><%= internship.duraton %></td>
        <td><%= internship.deadline %></td>
        <td><%= internship.website %></td>
        <td><%= link_to 'Show',internship %></td>
        <td><%= link_to "upvote",like_internship_path(internship),method: :put %></td>
        <td><%= link_to "downvote",dislike_internship_path(internship),method: :put %></td>
我的路线

Rails.application.routes.draw do
  devise_for :students
  resources :internships do
     member do
     put "like",to: "internships#upvote"
     put "dislike",to: "internships#downvote"
   end
 end
  devise_for :companies

到目前为止,效果很好!我想做的最后一件事是让学生展示他们已经推荐的实习机会,而我完全陷入困境。我是否必须将其放在新的控制器和视图中。我设计出了学生模型。朝着正确方向提供的任何帮助都将非常有用,非常感谢

cb943527 回答:如何将喜欢或赞好的帖子显示为可食用的宝石

为了获得您的“显示”链接,我想您想做以下事情:

<% @internships.each do |internship| %>
  <tr>
    <td><%= internship.title %></td>
    <td><%= internship.requirements %></td>
    <td><%= internship.location %></td>
    <td><%= internship.duraton %></td>
    <td><%= internship.deadline %></td>
    <td><%= internship.website %></td>
    <td><%= link_to 'Show',internship_path(internship) %></td>
    <td><%= link_to "upvote",like_internship_path(internship),method: :put %></td>
    <td><%= link_to "downvote",dislike_internship_path(internship),method: :put %></td>
  </tr>
<% end %>

您的before_action :set_internship,only: [:show,:edit,:update,:destroy]中似乎已经有InternshipsController。因此,我想您只需要制作一个show.html.erb

,

您可以在Internship上创建一个实例方法,该方法可以查看学生是否投票赞成它,并将其显示在索引表中。 internship.voted_for_by?(current_student),然后进入实习模式

def voted_for_by?(current_student)
  voting_student = votes_for.up.by_type(Student).voters.find_by(id: current_student.id)
  voting_student.blank?
end

由于我不太熟悉acts_as_votable,因此查询可能需要工作,但是您可以从中进行工作。然后根据布尔返回值,您可以显示支票或任何您想要表示赞成票的内容

本文链接:https://www.f2er.com/3162847.html

大家都在问