在生产中使用“文件上载/文件”字段提交表单时出现奇怪的路由行为

时间:2019-02-12 16:38:57

标签: controller routes ruby-on-rails-5 render carrierwave

在我的应用程序中,我使用carrierwave进行图像上传。

我的CampaignsController是平常的create action

def create
  @campaign = Campaign.new(campaign_params)

  respond_to do |format|
    if @campaign.save
      format.html { redirect_to @campaign, notice: 'Campaign was successfully created.' }
      format.json { render :show, status: :created, location: @campaign }
    else
      format.html { render :new }
      format.json { render json: @campaign.errors, status: :unprocessable_entity }
    end
  end
end

development中一切正常,但是在提交/保存后production中一切正常。

我的意思是: 在development中,提交表单后,保存到localhost:3000/campaigns/38,我的表单是:action="campaigns/38"

生产中,提交表单后,保存后,它会转到example.com/campaigns/38,而不是转到example.com/campaigns/campaigns/38,而我的表单将变为:action="campaigns/campaigns/38"

您会看到URL中有额外的campaigns,但我不确定为什么。

这是我的routes.rb

Rails.application.routes.draw do
  resources :campaigns do
    get 'changes', to: 'campaigns#deactivate_activate'
    put 'changes', to: 'campaigns#deactivate_activate'
  end

  # match '*any' => 'application#options', :via => [:options]
end

这是我的ImageUploader

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  storage :file

  def store_dir
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :large do
    resize_to_limit(600, 600)
  end

我的Campaign模型:

class Campaign < ApplicationRecord
  mount_uploader :image, ImageUploader

这是我的应用程序版本:

ruby "2.3.1"
gem 'rails', '~> 5.1.6'
carrierwave (1.3.1)

我一直在用不同的东西进行测试,以查看到底是什么问题。我使用simple_form_forform_for进行了测试,甚至删除了form的一部分。

只要= f.file_field :image不存在,一切都会起作用。当我从= f.file_field :image中删除form时,在所有ENV中一切正常,但是当我重新添加它时,它比Production时有这种奇怪的行为。

感谢您的任何帮助,并在此先感谢您!

0 个答案:

没有答案