在当地的机械工作中,工作完美。但是,当我在Heroku上部署时,它无法正常工作。
attachment_controller.rb:
class AttachmentsController < ApplicationController
before_action :authenticate_user!
def create
@project = Project.find(params[:project_id])
@task = @project.tasks.find(params[:task_id])
@attachment = @task.attachments.build(attachment_params)
if @attachment.save
redirect_to project_task_url(@project, @task)
else
redirect_to [@project, @task]
end
end
def destroy
@project = Project.find(params[:project_id])
@task = @project.tasks.find(params[:task_id])
@attachment = @task.attachments.find(params[:id])
@attachment.destroy
redirect_to project_task_url(@project, @task)
end
def download
attachment = Attachment.find(params[:id])
send_file attachment.document.path,
:filename => attachment.document_file_name,
:type => attachment.document_content_type,
:disposition => 'attachment'
end
private
def attachment_params
params.require(:attachment).permit(:description, :document)
end
end
而不是重定向到 /项目/ 1 /任务/ 3 /
他们重定向到 /项目/ 1 /任务/ 3 /附件
routes.rb中:
Rails.application.routes.draw do
devise_for :users
root 'static_pages#home'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
resources :projects do
resources :tasks, except: :index do
resources :attachments, only: [:create, :destroy]
end
end
match 'download/:id' => 'attachments#download', :as => :download, via: [:get, :post]
end
任务/ show.html.erb:
...
<!-- ATTACHMENTS -->
<h3>Attachments</h3>
<% if !@attachments.empty? %>
<% @attachments.each do |attachment| %>
<%= attachment.description %> |
<%= link_to 'Download', download_path([attachment.task.project.id, attachment.task.id, attachment.id]) %> |
<%= link_to "Preview", attachment.document.url(:original, false) %><br>
<% end %>
<% else %>
<p>Add an attachment</p>
<% end %>
<br>
<!-- FORM FOR ATTACHMENTS -->
<%= form_for([@project, @task, @attachment], multipart: true) do |f| %>
<div class="form-group">
<%= f.label :description %>
<%= f.text_field :description, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :document %>
<%= f.file_field :document, class: 'form-control'%>
</div>
<%= f.submit 'Upload', class: 'btn btn-primary' %>
<% end %>
task_controller:
class TasksController < ApplicationController
before_action :authenticate_user!
def show
@project = Project.find(params[:project_id])
@task = @project.tasks.find(params[:id])
@attachment = @task.attachments.build
@attachments = @task.attachments
end
production.rb:
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
和heroku变量已配置。 有人能帮助我吗?