我的应用程序的投票按钮存在问题,但该问题仅在生产中出现。投票按钮在开发中(本地)有效,但是当我部署应用程序时,投票按钮仍然没有反应。单击后,它不执行任何操作,heroku 服务器日志中也没有任何消息或指示。事实上,服务器日志中没有任何内容显示单击了任何内容。我查看了我的“_vote.html.erb”部分代码,发现其中没有任何问题。然而,我怀疑问题可能来 self 在部分中实现的 glyphicon Bootstrap 组件。我尝试更改代码,但没有成功。还是同样的问题。这是“votes/vote.html.erb”部分的内容:
<% if bookmark.votes.find_by(user: current_user).nil? %>
<button type="button" class="btn btn-info btn-sm" aria-label="Left Align">
<%= link_to "Like-bookmark", topic_bookmark_votes_path(topic, bookmark), method: :post, class: "glyphicon glyphicon-star"%>
</button>
<% else %>
<button type="button" class="btn btn-info btn-sm" aria-label="Left Align">
<%= link_to "Unlike-bookmark", topic_bookmark_vote_path(topic, bookmark, bookmark.votes.find_by(user: current_user)), method: :delete, class: "glyphicon glyphicon-star-empty" %>
</button>
<% end %>
从“_bookmark.html.erb”部分调用“_vote.html.erb”部分,以便它可以为每个书签呈现投票功能。这是“bookmarks/bookmark.html.erb”部分的内容
`
<% if defined?(upvoted_bookmark) && current_user.bookmarks.include?(upvoted_bookmark) %>
<div class="panel panel-danger">
<div class="panel-heading">
<div class="caption">
<h5>Created by <%= upvoted_bookmark.topic.user.name || upvoted_bookmark.topic.user.email %> on <%= upvoted_bookmark.created_at %></h5>
<br>
</div>
<div id="bkmak_url">
<%= link_to upvoted_bookmark.url, topic_bookmark_path(upvoted_bookmark.topic, upvoted_bookmark) %>
</div>
<% if policy(Vote.new).create? %>
<%= render partial: "votes/vote", locals: {topic: upvoted_bookmark.topic, bookmark: upvoted_bookmark} %>
<br>
<% end %>
</div>
</div>
<% else %>
<div class="caption">
<h5>Created by <%= bookmark.topic.user.name || bookmark.topic.user.email %> on <%= bookmark.created_at %></h5> <br>
</div>
<div id="bkmak_url" class="btn btn-default btn-xs active">
<%= link_to bookmark.url, topic_bookmark_path(bookmark.topic, bookmark) %>
</div>
<% if policy(bookmark).destroy? %>
<div class="btn btn-primary btn-md active btn-block">
<%= link_to "delete bookmark", topic_bookmark_path(bookmark.topic, bookmark), method: :delete, data: { confirm: "Are you sure you want to delete this bookmark?" } %>
</div>
<% end %>
<% end %>
Like i said before, the issue is only in production. It works perfectly in development (locally). I ran
heroku run rake db:migrate` 以确保我的生产数据库架构已正确设置。我在开发中运行 sqlite3,在生产中运行 progresql(我知道这并不总是可取的)。如果您需要任何进一步的代码,请告诉我,并提前感谢您的善意和周到的帮助。
更新: =============================================== ===================
@Siguza,谢谢。不,我在服务器日志或控制台中没有任何 JS 错误。然而,最终对我有用的是围绕 glyphicon 组件实现进行更改,如下所示
<% if bookmark.votes.find_by(user: current_user).nil? %>
<div class="btn btn-info">
<%= link_to "Like-bookmark", topic_bookmark_votes_path(topic, bookmark), method: :post, class: "glyphicon glyphicon-star" %>
</div>
<% else %>
<div class="btn btn-warning">
<%= link_to "Unlike-bookmark", topic_bookmark_vote_path(topic, bookmark, bookmark.votes.find_by(user: current_user)), method: :delete, class: "glyphicon glyphicon-star-empty" %>
</div>
<% end %>
这使得投票按钮在生产中正常工作。
最佳答案
这可能是 Assets 预编译的问题。我建议您查看heroku的预编译指南https://devcenter.heroku.com/articles/rails-4-asset-pipeline .
最后,启用服务静态资源
# config/application.rb
config.serve_static_assets = true
添加rails_12factor
gem
# Gemfile
gem 'rails_12factor', group: :production
然后预编译您的资源
$ rake assets:precompile
提交更改,包括您的 public/assets
文件夹并将其推送到 heroku。
关于ruby-on-rails - Ruby on Rails——Heroku : Issue with votes button only in production,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31772578/