Ruby on Rails - Heroku:仅在生产中使用投票按钮发出问题

时间:2015-08-02 13:55:11

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

我的应用投票按钮存在问题,但问题仅出在制作中。投票按钮在开发(本地)中工作,但是当我部署应用程序时,投票按钮仍然不昂贵。单击时,它不执行任何操作,并且heroku服务器日志中没有消息或指示。事实上,服务器日志中没有任何内容显示已点击任何内容。我查看了我的“_vote.html.erb”部分代码,我发现其中没有任何问题。但是我怀疑这个问题可能来自我在部分中实现的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运行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 %> 这使投票按钮在生产中正常工作。

1 个答案:

答案 0 :(得分:2)

这可能是资产预编译的问题。我建议您查看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。