ruby-on-rails - Ruby on Rails——Heroku : Issue with votes button only in production

标签 ruby-on-rails twitter-bootstrap heroku partial-views glyphicons

我的应用程序的投票按钮存在问题,但该问题仅在生产中出现。投票按钮在开发中(本地)有效,但是当我部署应用程序时,投票按钮仍然没有反应。单击后,它不执行任何操作,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/

相关文章:

heroku - Heroku 会忽略 .env 文件吗?

ruby-on-rails - 无法将 Rails 应用程序部署到 Heroku, "Invalid RUBY_VERSION"

ruby-on-rails - 用以前的加密设计

ruby-on-rails - 更新生产服务器上的代码,但浏览器上没有变化

ruby-on-rails - 如何继续 Ruby 中的下一个救援 block ?

css - 使用 Bootstrap,我可以在没有 'center-block' 的情况下将 DIV 居中吗?

ruby-on-rails - 找到流浪者放入​​ ruby 代码

html - 轮播不适用于 bootstrap v4

twitter-bootstrap - 使用 twitter bootstrap,如何使嵌入式视频响应?

django - 如何将 PyCharm 连接到 Heroku postgres 数据库