首先,我使用的gem是https://github.com/ryanto/acts_as_votable
你好啊!我是rails的新手,正在尝试构建一个学生可以喜欢公司发布的实习机会的应用程序。到目前为止,我已经做好了所有的工作,但我希望学生能够看到他们支持的公司的帖子。
到目前为止,这是我的实习控制器,它是使用scaffolding创建的/没有包含所有内容。
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
到目前为止,这个方法运行得很好!我最不想做的事情就是让学生展示他们的实习机会,而我完全被卡住了。我必须把它放在一个新的控制器和视图中吗?我用devise生成了我的学生模型。在正确的方向上的任何帮助都会很好,非常感谢
发布于 2019-11-05 11:53:52
为了获得你的“Show”链接,我想你应该这样做:
<% @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 %>看起来您的InternshipsController中已经有了before_action :set_internship, only: [:show, :edit, :update, :destroy]。所以,我想你只需要做一个show.html.erb。
发布于 2019-11-05 16:58:43
您可以在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://stackoverflow.com/questions/58703223
复制相似问题