我试图创建一个简单的喜欢/不像Rails 4按钮。我试着用社会化宝石做到这一点,但经过一天的挣扎,我放弃了,并决定修改M. Hartl的"愚"机制从Rails教程。以下是我目前得到的:
User.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :validatable
has_many :questions
has_many :answers
has_many :likes, foreign_key: "liker_id", dependent: :destroy
has_many :liked_answers, through: :likes, source: :liked, source_type: "Answer"
def like?(answer)
likes.find_by(liked_id: answer.id)
end
def like!(answer)
likes.create!(liked_id: answer.id)
end
def unlike!(answer)
likes.find_by(liked_id: answer.id).destroy
end
end
Answer.rb:
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
has_many :likes, foreign_key: "liked_id",
class_name: "Like",
dependent: :destroy
has_many :likers, through: :likes, source: :liker
end
Like.rb:
class Like < ActiveRecord::Base
belongs_to :liker, class_name: "User"
belongs_to :liked, class_name: "Answer"
validates :liker_id, presence: true
validates :liked_id, presence: true
end
likes_controller.rb:
class LikesController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :authenticate_user!
respond_to :html, :js
def create
@answer = Answer.find(params[:like][:liked_id])
current_user.like!(@answer)
respond_with @answer.question
end
def destroy
@answer = Like.find(params[:id]).liked
current_user.unlike!(@answer)
respond_with @answer.question
end
end
_like.html.erb:
<%= form_for(current_user.likes.build(liked_id: @answer.id), remote: true) do |f| %>
<div><%= f.hidden_field :liked_id %></div>
<%= f.submit "Like!", class: "btn btn-large btn-primary" %>
<% end %>
_unlike.html.erb:
<%= form_for(current_user.likes.find_by(liked_id: @answer.id),
html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Unlike.", class: "btn btn-large" %>
<% end %>
routes.rb:
Rails.application.routes.draw do
devise_for :users
scope "/admin" do
resources :users
end
resources :questions do
resources :answers do
get :likes
end
end
resources :likes, only: [:create, :destroy]
root to: 'questions#index'
end
我也有jQuery和jQuery_ujs所需的application.js,和相关的js。视图中的动词文件("create"one_answers"destroy")。
"喜欢/不喜欢"机制本身似乎工作得很好-在控制台,我的"喜欢!"one_answers"不像!"方法,我能够创建和销毁"喜欢"对象与id, liker_id和liked_id。
问题从按钮本身开始。
我可以看到"喜欢!"按钮旁边的每个答案,但当我点击它-我得到这个错误:
ActiveRecord::RecordNotFound in LikesController#create
Couldn't find Answer with 'id'=
LikesController:
这一行的错误点@answer = Answer.find(params[:like][:liked_id])
所以我想我的@答案。id结果为'nil',但我不知道我在哪里犯了错误。我的第一个猜测是路由文件-我仍然不确定是否一切都是正确的。
我花了一整天的时间寻找解决方案,我也在SO上发现了一些类似的问题,但是没有一个答案可以帮助我。
如有任何意见,不胜感激。
编辑:来自错误的参数
Parameters:
{"utf8"=>"✓",
"like"=>{"liked_id"=>""},
"commit"=>"Like!"}
您使用了错误的隐藏字段标签。http://api.rubyonrails.org/v4.1.1/classes/ActionView/Helpers/FormHelper.html#method-i-hidden_field显示需要在标记中提供两个值。将您的like ERB文件更改为:
_like.html.erb:
<%= form_for(current_user.likes.build(liked_id: @answer.id), remote: true) do |f| %>
<div><%= f.hidden_field :liked_id, :value => @answer.id %></div>
<%= f.submit "Like!", class: "btn btn-large btn-primary" %>
<% end %>
,这应该得到你想要的