开发中的Rails POST(补丁)在生产中成为GET

时间:2019-05-05 19:43:52

标签: ruby-on-rails ruby docker http

Rails link which POSTs in development GETs in production中的情况相同,但是该问题没有结论,因此我也在尝试:

  • Rails 5.2.3
  • Ruby 2.6
  • 在Docker容器中运行
  • 实时应用程序在Ubuntu 16.04.6上

我认为以下内容:

<%= link_to '&#10003;'.html_safe, item_path(item), method: :patch, :class=>"item__buy-btn" %>

它生成以下HTML:

<a class="item__buy-btn" rel="nofollow" data-method="patch" href="/items/32">✓</a>

这在我的项目控制器中(我没有项目#show):

def update
  item = Item.find(params[:id])
  item.update(bought: true)
  list = List.find(item.grocery_item_id)
  receipt = Receipt.find(list.receipts.last.id)
  receipt.close
  item.update(grocery_item_id: receipt.id, grocery_item_type: receipt.class.name)
  item.save
  redirect_to request.referrer || root_url
end

这是我与物品相关的路线:

                items GET    /items(.:format)                                                                         items#index
                      POST   /items(.:format)                                                                         items#create
             new_item GET    /items/new(.:format)                                                                     items#new
            edit_item GET    /items/:id/edit(.:format)                                                                items#edit
                 item GET    /items/:id(.:format)                                                                     items#show
                      PATCH  /items/:id(.:format)                                                                     items#update
                      PUT    /items/:id(.:format)                                                                     items#update
                      DELETE /items/:id(.:format)                                                                     items#destroy

显然是POST: response in development

日志显示PATCH:

web_1              | Started PATCH "/items/33" for 172.21.0.1 at 2019- 05-05 19:29:31 +0000
web_1              | Cannot render console from 172.21.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
web_1              | Processing by ItemsController#update as HTML
web_1              |   Parameters: {"authenticity_token"=>"KmPf0fEDEs6Y2gUmC6Su9fdeVOg51AwCUUWyQBq14Gz0Fs6IRJ5cmmQdUETMbE+IMRTTGcfQPQ86x0rNzDSRHg==", "id"=>"33"}

然后在PRODUCTION中:

I, [2019-05-05T18:27:22.439552 #1]  INFO -- : [93c90971-1378-4e39-b6fb-8329dd46922e] Started GET "/" for 10.255.0.2 at 2019-05-05 18:27:22 +0000
I, [2019-05-05T18:27:22.440379 #1]  INFO -- : [93c90971-1378-4e39-b6fb-8329dd46922e] Processing by HomeController#index as HTML
I, [2019-05-05T18:27:22.441694 #1]  INFO -- : [93c90971-1378-4e39-b6fb-8329dd46922e]   Rendering home/index.html.erb within layouts/application
I, [2019-05-05T18:27:22.443279 #1]  INFO -- : [93c90971-1378-4e39-b6fb-8329dd46922e]   Rendered home/index.html.erb within layouts/application (1.4ms)
I, [2019-05-05T18:27:22.444679 #1]  INFO -- : [93c90971-1378-4e39-b6fb-8329dd46922e] Completed 200 OK in 4ms (Views: 3.0ms)
I, [2019-05-05T18:49:37.971732 #1]  INFO -- : [5f452d24-80c5-44dc-bd8a-cb2be0aa5f14] Started GET "/items/7" for 10.255.0.2 at 2019-05-05 18:49:37 +0000
F, [2019-05-05T18:49:37.973154 #1] FATAL -- : [5f452d24-80c5-44dc-bd8a-cb2be0aa5f14]
F, [2019-05-05T18:49:37.973209 #1] FATAL -- : [5f452d24-80c5-44dc-bd8a-cb2be0aa5f14] AbstractController::ActionNotFound (The action 'show' could not be found for ItemsController):
F, [2019-05-05T18:49:37.973237 #1] FATAL -- : [5f452d24-80c5-44dc-bd8a-cb2be0aa5f14]
F, [2019-05-05T18:49:37.973274 #1] FATAL -- : [5f452d24-80c5-44dc-bd8a-cb2be0aa5f14] actionpack (5.2.3) lib/abstract_controller/base.rb:129:in `process'

response in production

什么可能导致此行为? 我在这篇文章中缺少任何相关信息吗?

编辑:

按照ser tadmans的回答,这显然与javascript有关。 由于其中一个文件中有'#'注释,因此预编译失败。

1 个答案:

答案 0 :(得分:2)

通常会发生这种情况,因为Rails UJS JavaScript库无法加载并且其注入的数据属性被忽略了。

检查是否已加载UJS库。如果您正在使用Sprockets,请确保显示rails-ujs中的application.js“注释”行。

如果您以production模式运行,则需要确保正确编译资产。可以使用rake assets:precompile在Rails 3到5.2的程序部署位置中完成。