簡體   English   中英

remote:真正的Ajax調用可在本地服務器中使用,但不適用於Heroku

[英]remote: true Ajax call works in local server but not in Heroku

這是我對堆棧溢出的第一個問題。

我正在編寫將在Heroku中運行的Rails應用程序。 用戶可以喜歡/不喜歡帖子。 此操作已由推薦的gem的Sidekiq工作人員注冊。

我正在使用“公共活動”來跟蹤帖子的活動,因此a.trackable.id是post.id ...

我在本地主機中的以下代碼中有ajax調用:

public_activity / posts / _create.html.erb:

<%= link_to "Beğenmekten Vazgeç", unlike_post_path(a.trackable.id), data: {id: a.trackable.id, toggle_text: 'Beğen', toggle_href: like_post_path(a.trackable.id)}, class: 'like_toggle', remote: true %>

在我的Posts Controller中:

def like
  @post = Post.find(params[:id])
    current_user.like(@post)

  if request.xhr?
    render json: { id: @post.id }
  else
    redirect_to user_profile_path(@post.user)
    flash[:notice] = "Ajax hatası"
  end
end

和_create.coffee文件是

$(document).on 'ajax:success', 'a.like_toggle', (status, data, xhr) ->
  $("a.like_toggle[data-id=#{data.id}]").each ->
    $a = $(this)
    href = $a.attr 'href'
    text = $a.text()
    $a.text($a.data('toggle-text')).attr 'href', $a.data('toggle-href')
    $a.data('toggle-text', text).data 'toggle-href', href
    return

本質上,它會更改問題中描述的喜歡/不喜歡的文本。

在我的gemfile中,我有

gem 'jquery-rails', '~> 4.0.3'

在我的application.js文件中;

//= require jquery
//= require jquery_ujs
//= require jquery-ui

我清理並預編譯了生產中的資產。

在本地主機上運行正常。 但是在Heroku中,我似乎無法正常工作。

這是服務器日志:

Processing by PostsController#like as HTML 
Parameters: {"id"=>"1"}
User Load (1.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Post Load (1.6ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1  [["id", 1]]
Redirected to https://emoportal.herokuapp.com/user_profiles/1
2015-04-24T16:14:15.287Z 3 TID-osdy24n1g Recommendable::Workers::Sidekiq JID-986cd2fea4f7bfffd55849b0 INFO: start
2015-04-24T16:14:15.368Z 3 TID-osdy24n1g Recommendable::Workers::Sidekiq JID-986cd2fea4f7bfffd55849b0 INFO: done: 0.082 sec
User Load (1.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
Completed 302 Found in 680ms (ActiveRecord: 6.7ms)

任何幫助或想法將不勝感激。 謝謝。

解決了。 問題是,盡管進行了所有預編譯和其他工作,但在Heroku環境中不存在jquery_ujs。

在config / environments / production.rb文件中,我將config.serve_static_files = true設置config.serve_static_files = true RAILS_ENV=production rake assets:precompile然后將其推送回購到Heroku。

就是這樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM