我有一个ruby应用程序,其中有帖子和类似模型。我希望能够单击一个喜欢按钮,然后刷新该帖子的喜欢数目。为此,我正在使用ajax。
我的问题是我如何才能在我的部分html文档中计算帖子的点赞次数。目前,我的_show.html.erb中的代码如下:
<%= @post.likes.all.count %>
我要在index.html.erb中显示代码的div看起来像这样:
<div>
Likes: <div id="<% post.id %>_likes_output", local: :post></div>
</div>
我的create.js.erb看起来像这样:
$(document.getElementById("<%@post%> _likes_output").innerHTML = "<%= render partial: "likes/show", local: :post %>");
“我的喜欢”创建方法如下:
def create
@user = current_user.id
@post = params[:post_id]
likes = {user_id: @user, post_id: @post}
@like = Like.new(likes)
# a new entry in the likes table gets created
if @like.save!
else
flash[:alert] = "Something went wrong"
end
end
此刻我出现以下错误:
Started POST "/posts/13/likes" for ::1 at 2019-03-22 11:38:28 +0100
Processing by LikesController#create as JS
Parameters: {"post_id"=>"13"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/likes_controller.rb:4
(0.0ms) begin transaction
↳ app/controllers/likes_controller.rb:10
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 13], ["LIMIT", 1]]
↳ app/controllers/likes_controller.rb:10
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/likes_controller.rb:10
Like Create (0.3ms) INSERT INTO "likes" ("created_at", "updated_at", "user_id", "post_id") VALUES (?, ?, ?, ?) [["created_at", "2019-03-22 10:38:28.067946"], ["updated_at", "2019-03-22 10:38:28.067946"], ["user_id", "1"], ["post_id", "13"]]
↳ app/controllers/likes_controller.rb:10
(1.0ms) commit transaction
↳ app/controllers/likes_controller.rb:10
Rendering likes/create.js.erb
Rendered likes/_show.html.erb (21.0ms)
Rendered likes/create.js.erb (38.8ms)
Completed 500 Internal Server Error in 91ms (ActiveRecord: 1.8ms)
ActionView::Template::Error (undefined method `likes' for "13":String
Did you mean? lines):
1: <%= @post.likes.all.count %>
app/views/likes/_show.html.erb:1:in `_app_views_likes__show_html_erb___2777362880017156755_70224479796840'
app/views/likes/create.js.erb:3:in `_app_views_likes_create_js_erb__712898782247662482_70224408518740'
请让我知道您是否还需要其他东西。非常感谢!
答案 0 :(得分:2)
@ flo3719您的代码中可能还存在其他问题,但是您提到的ActionView::Template::Error (undefined method 'likes' for "13":String
是由罪魁祸首行@post = params[:post_id]
引起的,由于这一行,您的@post
对象包含{{ 1}}。
您应该使用以下方法从数据库中找到您的帖子:
"13"
用下面的代码替换@post = Post.find_by_id(params[:post_id])
@post = params[:post_id]
答案 1 :(得分:1)
您的<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="mobile-nav" class="sidenav" style="height: 0;">
<div id="mobile-nav-content">
<nav>
<ul>
<li><a href="#home">Casa</a></li>
<li><a href="#about">Sobre Mi</a></li>
<li><a href="#services">Servicio</a></li>
<li><a href="#gallery">Galería</a></li>
<li><a href="#contact">Contacto</a></li>
</ul>
</nav>
</div>
</div>
<div id="desktop-navbar">
<div id="desktop-nav-wrapper">
<h3 id="desktop-logo" class="logo"><a href="/">Logo </a></h3>
<nav>
<ul id="desktop-nav-content">
<li id="mobile-menu-link"><a>Menu</a></li>
</ul>
</nav>
</div>
</div>
似乎是一个字符串。在这里
@post
应该是
@post = params[:post_id]
答案 2 :(得分:0)
您应该找到自己的帖子,而不要传递id:
@post = Post.find(params[:post_id])
另外,考虑使用counter_cache存储点赞的次数-就性能而言要好得多! https://guides.rubyonrails.org/association_basics.html#options-for-belongs-to-counter-cache