我无法下载我上传的文件,当我点击该文件时,我收到错误“无法找到没有ID的简报”。这是我的代码
class NewslettersController < ApplicationController
before_action :set_newsletter, only: [:show, :edit, :update, :destroy]
# GET /newsletters
# GET /newsletters.json
def index
@newsletters = Newsletter.all
end
# GET /newsletters/1
# GET /newsletters/1.json
def show
end
# GET /newsletters/new
def new
@newsletter = Newsletter.new
end
# GET /newsletters/1/edit
def edit
end
# POST /newsletters
# POST /newsletters.json
def create
@newsletter = Newsletter.new(newsletter_params)
respond_to do |format|
if @newsletter.save
format.html { redirect_to @newsletter, notice: 'Newsletter was successfully created.' }
format.json { render action: 'show', status: :created, location: @newsletter }
else
format.html { render action: 'new' }
format.json { render json: @newsletter.errors, status: :unprocessable_entity }
end
end
end
#file download
def document_download
@newsletter = Newsletter.find(params[:id])
file_path = @newsletter.document_download
if !file_path.nil?
send_file "#{Rails.root}/public/system/documents#{params[:id]}/original/#{file_path}", :x_sendfile => true
else
redirect_to newsletters_url
end
end
# PATCH/PUT /newsletters/1
# PATCH/PUT /newsletters/1.json
def update
respond_to do |format|
if @newsletter.update(newsletter_params)
format.html { redirect_to @newsletter, notice: 'Newsletter was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @newsletter.errors, status: :unprocessable_entity }
end
end
end
# DELETE /newsletters/1
# DELETE /newsletters/1.json
def destroy
@newsletter.destroy
respond_to do |format|
format.html { redirect_to newsletters_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_newsletter
@newsletter = Newsletter.find(params[:newsletter_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def newsletter_params
params.require(:newsletter).permit(:document, :year)
end
end
<tbody>
<% @newsletters.each do |newsletter| %>
<tr>
<td><a href="/public/newsletters/document_download/", style="text-decoration:none"> <%=newsletter.document_file_name %></a></td>
<td><%= newsletter.year %></td>
<td><%= link_to 'Show', newsletter %></td>
<td><%= link_to 'Edit', edit_newsletter_path(newsletter) %></td>
<td><%= link_to 'Destroy', newsletter, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
<p id="notice"><%= notice %></p>
<p>
<strong>Document:</strong>
<%= document_download_newsletter_path(@id_or_model_itself) %>
</p>
<p>
<strong>Year:</strong>
<%= @newsletter.year %>
</p>
<%= link_to 'Edit', edit_newsletter_path(@newsletter) %> |
<%= link_to 'Back', newsletters_path %>
<%= form_for(@newsletter) do |f| %>
<% if @newsletter.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@newsletter.errors.count, "error") %> prohibited this newsletter from being saved:</h2>
<ul>
<% @newsletter.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :document %><br>
<%= f.text_field :document %>
<%= f.file_field :document %>
</div>
<div class="field">
<%= f.label :year %><br>
<%= f.text_field :year %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
resources :newsletters do
get :document_download, on: :member
end
class Newsletter < ActiveRecord::Base
has_attached_file :document
validates_attachment_presence :document
validates_attachment_content_type :document, :content_type => [ 'application/pdf','text/plain']
end
答案 0 :(得分:0)
没有阅读您的完整代码,但根据您提到的错误,问题出在您的set_newsletter
方法中。由于这是NewslettersController
,并且假设您没有为Newsletter
模型手动设置不同的主键,您应该使用params[:id]
而不是params[:newsletter_id]
,如下所示:
# app/controllers/newsletters_controller.rb
def set_newsletter
@newsletter = Newsletter.find(params[:id])
end
答案 1 :(得分:0)
更新回复(2013年12月1日):
根据我们的评论,我再次查看了您的代码并在index.html中看到了这一行(我假设是index.html.erb):
<td><a href="/public/newsletters/document_download/", style="text-decoration:none"> <%=newsletter.document_file_name %></a></td>
<td><%= newsletter.year %></td>
看起来您指的是简报文档名称,但未在href中传递简报ID。你的show.html(show.html.erb)似乎是正确的,但我看不到你在哪里初始化“@id_or_model_itself”的值。
您可以修改这些部件并验证它是否有效吗?
原始回复
在“set_newsletter”方法中,您尝试使用“params [:newsletter_id]”来获取简报的ID,但是在“document_download”操作的情况下,您正在使用“params [:id]”。我怀疑这就是问题背后的原因。尝试在document_download中使用“params [:newsletter_id]”。