我喜欢和不喜欢有效的按钮,但不止一次投票的错误信息。
用户可以投票的对象类型称为课程。我列出了课程索引页面上的所有课程以及喜欢/不喜欢的按钮。我试图这样做,如果你试图喜欢或不喜欢同一个课程对象两次,页面会显示一条Flash消息,通知你只能投票一次。
这是喜欢的控制器。
#likeables_controller.rb
class LikeablesController < ApplicationController
before_action :logged_in_user
def create
course = Course.find(params[:liked_id])
count = course.likers.count
current_user.like(course)
new_count = course.likers.count
if new_count > count
redirect_to courses_url
else
# This isn't working
flash[:danger] = "You can only vote on a course once!"
redirect_to courses_url
end
end
def destroy
like = Likeable.find(params[:id])
like.destroy
redirect_to courses_url
end
end
我的课程索引是部分渲染课程:
# index.html.erb
<%= render @courses %>
课程部分依次呈现喜欢和不喜欢的按钮
# _course.html.erb
<ul class="stats">
<li>
<%= render partial: 'dislike', locals: {course: course} %>
</li>
<li>
<%= render partial: 'like', locals: {course: course} %>
</li>
</ul>
# _like.html.erb
<%= form_for(current_user.likeables.build, remote: true) do |f| %>
<div><%= hidden_field_tag :liked_id, course.id %></div>
<%= button_tag type: 'submit', class: "btn" do %>
<i class="fa fa-thumbs-o-up" aria-hidden="true"></i>
<span id="likers"><%= course.likers.count %></span>
<% end %>
<% end %>
并且需要它,这是我的模型和架构的相关部分:
create_table "dislikeables", force: :cascade do |t|
t.integer "disliker_id"
t.integer "disliked_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["disliked_id"], name: "index_dislikeables_on_disliked_id"
t.index ["disliker_id", "disliked_id"],
name: "index_dislikeables_on_disliker_id_and_disliked_id", unique: true
t.index ["disliker_id"], name: "index_dislikeables_on_disliker_id"
end
create_table "likeables", force: :cascade do |t|
t.integer "liker_id"
t.integer "liked_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["liked_id"], name: "index_likeables_on_liked_id"
t.index ["liker_id", "liked_id"],
name: "index_likeables_on_liker_id_and_liked_id", unique: true
t.index ["liker_id"], name: "index_likeables_on_liker_id"
end
我的模特:
# user.rb
class User < ApplicationRecord
# User has many courses
has_many :courses, dependent: :destroy
has_many :likeables, foreign_key: "liker_id",
dependent: :destroy
has_many :likes, through: :likeables, source: :liked
has_many :dislikeables, foreign_key: "disliker_id",
dependent: :destroy
has_many :dislikes, through: :dislikeables, source: :disliked
def like(course)
likes << course
end
def dislike(course)
dislikes << course
end
end
# course.rb
class Course < ApplicationRecord
has_one :user
has_many :likeables, foreign_key: "liked_id",
dependent: :destroy
has_many :likers, through: :likeables, source: :liker
has_many :dislikeables, foreign_key: "disliked_id",
dependent: :destroy
has_many :dislikers, through: :dislikeables, source: :disliker
def liker?(user)
likes.include?(user)
end
def disliker?(user)
dislikes.include?(user)
end
end
# likeable.rb
class Likeable < ApplicationRecord
belongs_to :liked, class_name: "Course"
belongs_to :liker, class_name: "User"
validates :liked_id, presence: true
validates :liker_id, presence: true
end
# dislikeable.rb
class Dislikeable < ApplicationRecord
belongs_to :disliked, class_name: "Course"
belongs_to :disliker, class_name: "User"
validates :disliked_id, presence: true
validates :disliker_id, presence: true
end
答案 0 :(得分:1)
您的行动存在问题,
def create
like = current_user.likes.where(liked_id: params[:liked_id]).first_or_initialize
if like.new_record? && like.save
redirect_to courses_url
else
redirect_to courses_url, flash: { danger: "You can only vote on a course once!" }
end
end
答案 1 :(得分:0)
也许在控制器中尝试:
redirect_to courses_url, flash: { danger: "You can only vote on a course once!" }
并确保在应用程序布局中,您有Flash消息的渲染器。
答案 2 :(得分:0)
试试这个
redirect_to courses_url, alert: "You can only vote on a course once!"