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