我有一个帖子的喜欢/不喜欢按钮的问题。Rails4:像按钮不能正常工作(像限制)
什么我想要做的:
用户有很多帖子,每个用户只能按1喜欢或不喜欢(取消等)平均每个职位。我怎样才能做到这一点?
我的问题:
我可以按按钮一样,它显示计数1等,但是不喜欢按钮不起作用(无法取消等)。此外,职位的限制是不正常工作,我可以按2或3喜欢一个职位(同一职位)。
补充说明:
所有职位由最热门的职位(喜欢的号码)排序,它应是受欢迎的页面。所以我有流行的行动来排序最受欢迎的职位的帖子。
def popular
@posts = Post.joins(:likes).group(:id).order("count(*)
desc").paginate(:page => params[:page], :per_page => 18)
end
喜欢表的列:
USER_ID,POST_ID,created_at,的updated_at
职位表中的列:
ID,标题,内容,USER_ID,created_at,的updated_at,图片,category_id,经度,纬度,地址
发展:sqlite3的生产:在PostgreSQL
post.rb
class Post < ActiveRecord::Base
belongs_to :user
belongs_to :category
validates :user_id, presence: true
validates :title, presence: true
validates :content, presence: true
validates :category_id, presence: true
has_many :likes, dependent: :destroy
has_many :post_attachments
attr_accessor :post_attachment_attributes
acts_as_commontable
geocoded_by :address
after_validation :geocode
accepts_nested_attributes_for :post_attachments,allow_destroy:true, reject_if: :all_blank
end
like.rb
class Like < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
likes_controller.rb
class LikesController < ApplicationController
def like
@post = Post.find(params[:post_id])
like = current_user.likes.build(post_id: @post.id)
like.save
end
def unlike
@post = Post.find(params[:post_id])
like = current_user.likes.find_by(post_id: @post.id)
like.destroy
end
end
_like_links.html.erb
<% if user_signed_in? %>
<% if current_user.likes.find_by(post_id: post.id) %>
<%= link_to (content_tag(:i, '', class: 'fa faheart')),unlike_path(post.id), method: :delete, remote: true %>
<%= post.likes.count %> Like
<% else %>
<%= link_to (content_tag(:i, '', class: 'fa fa-heart o')),like_path(post.id), method: :post, remote: true %>
<%= post.likes.count %> Like
<% end %>
<% else %>
<%= post.likes.count %> Like
<% end %>
like.js.erb/unlike.js.erb
$('#like-link<%= @post.id %>').html('<%=escape_javascript(render("likes/like_links", post: @post)) %>');
任何帮助将是并欣赏。谢谢你的帮助!!
更改后喜欢控制器
Started POST "/like/25" for ::1 at 2017-05-11 17:29:17 +0900
Processing by LikesController#like as JS
Parameters: {"post_id"=>"25"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE
"users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Like Load (0.2ms) SELECT "likes".* FROM "likes" WHERE
"likes"."user_id" = ? AND "likes"."post_id" = ? LIMIT 1
[["user_id", 1], ["post_id", 25]]
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "likes" ("user_id", "post_id",
"created_at", "updated_at") VALUES (?, ?, ?, ?) [["user_id", 1],
["post_id", 25], ["created_at", "2017-05-11 08:29:18.009414"],
["updated_at", "2017-05-11 08:29:18.009414"]]
(1.9ms) commit transaction
Rendered likes/like.js.erb (35.7ms)
Completed 500 Internal Server Error in 233ms (ActiveRecord: 17.4ms)
NoMethodError (undefined method `id' for nil:NilClass):
app/views/likes/like.js.erb:1:in
`_app_views_likes_like_js_erb___3930955212291711879_70111836109880'
的index.html。ERB
<% @posts.each do |post| %>
<div class="mdl-cell mdl-cell--6-col">
<div class="demo-card-square mdl-card mdl-shadow--2dp">
<div class="category category"><%= link_to post.category.name [post.category.name] %>
</div>
<div class="mdl-card__title mdl-card--expand" style="background: url('<%= post.post_attachments.first.picture %>') bottom center no-repeat #fff;">
<h2 class="mdl-card__title-text"><%= post.title %></h2>
</div>
<div class="mdl-card__supporting-text">
<%= post.content.truncate(120, separator: '.')%>
</div>
<div class="mdl-card__actions mdl-card--border">
<a href="<%= post_path(post.id) %>" class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect" data-upgraded=",MaterialButton,MaterialRipple">
View more
<span class="mdl-button__ripple-container"><span class="mdl-ripple"></span></span></a>
<span id="like-link<%= post.id %>" class="like_box">
<%= render "likes/like_links", post: post %>
</span>
</div>
</div>
</div>
<% end %>
马里兰州嗨感谢您的帮助!我改变了喜欢动作,但它仍然是相同的(不能取消像按钮和限制不工作)。 – arthur
它给出了什么错误?我认为应该是'@ post.id'而不是'post.id' –
'@ post'是零,对象没有设置。 –