上传和下载文件(Rails 4)

时间:2021-06-12 09:55:49

i am not able to download my uploaded file, when i click on the file i get the error "Couldn't find Newsletter without an id". this is my code

我无法下载我上传的文件,当我点击该文件时,我收到错误“无法找到没有ID的简报”。这是我的代码

controller.rb

     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

index.html

   <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>


show.html

     <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.html

    <%= 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 %>

routes.rb

      resources :newsletters do
    get :document_download, on: :member 

end

model(newsletters.rb)

   class Newsletter < ActiveRecord::Base

    has_attached_file :document
    validates_attachment_presence :document
    validates_attachment_content_type :document, :content_type => [ 'application/pdf','text/plain']


    end

2 个解决方案

#1


0  

Didn't read your full code but based on the error you've mentioned, the problem is in your set_newsletter method. Since this is NewslettersController and assuming you haven't manually set a different primary key for Newsletter model, you should be using params[:id] and not params[:newsletter_id] as follows:

没有阅读您的完整代码,但根据您提到的错误,问题出在您的set_newsletter方法中。由于这是NewslettersController并且假设您没有为Newsletter模型手动设置不同的主键,您应该使用params [:id]而不是params [:newsletter_id],如下所示:

# app/controllers/newsletters_controller.rb

def set_newsletter
  @newsletter = Newsletter.find(params[:id]) 
end

#2


0  

Updated response (1st December 2013):

更新回复(2013年12月1日):

Based on our comment, I again looked at your code and came across this line in index.html (index.html.erb I assume):

根据我们的评论,我再次查看了您的代码并在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>

It looks like you are referring to a newsletter document name but not passing the newsletter id in href. Your show.html (show.html.erb) seems to be correct, but I don't see where you are initializing the value for "@id_or_model_itself".

看起来您指的是简报文档名称,但未在href中传递简报ID。你的show.html(show.html.erb)似乎是正确的,但是我没有看到你在哪里初始化“@id_or_model_itself”的值。

Can you modify these parts and verify if it works?

你能修改这些部件并验证它是否有效吗?

Original Response

原始回应

In the "set_newsletter" method, you are trying to get the id of newsletter using "params[:newsletter_id]", but in case of "document_download" action, you are using "params[:id]". I suspect that is the reason behind the issue. Try to use "params[:newsletter_id]" in document_download as well.

在“set_newsletter”方法中,您尝试使用“params [:newsletter_id]”来获取新闻稿的ID,但是在“document_download”操作的情况下,您使用的是“params [:id]”。我怀疑这就是问题背后的原因。尝试在document_download中使用“params [:newsletter_id]”。

#1


0  

Didn't read your full code but based on the error you've mentioned, the problem is in your set_newsletter method. Since this is NewslettersController and assuming you haven't manually set a different primary key for Newsletter model, you should be using params[:id] and not params[:newsletter_id] as follows:

没有阅读您的完整代码,但根据您提到的错误,问题出在您的set_newsletter方法中。由于这是NewslettersController并且假设您没有为Newsletter模型手动设置不同的主键,您应该使用params [:id]而不是params [:newsletter_id],如下所示:

# app/controllers/newsletters_controller.rb

def set_newsletter
  @newsletter = Newsletter.find(params[:id]) 
end

#2


0  

Updated response (1st December 2013):

更新回复(2013年12月1日):

Based on our comment, I again looked at your code and came across this line in index.html (index.html.erb I assume):

根据我们的评论,我再次查看了您的代码并在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>

It looks like you are referring to a newsletter document name but not passing the newsletter id in href. Your show.html (show.html.erb) seems to be correct, but I don't see where you are initializing the value for "@id_or_model_itself".

看起来您指的是简报文档名称,但未在href中传递简报ID。你的show.html(show.html.erb)似乎是正确的,但是我没有看到你在哪里初始化“@id_or_model_itself”的值。

Can you modify these parts and verify if it works?

你能修改这些部件并验证它是否有效吗?

Original Response

原始回应

In the "set_newsletter" method, you are trying to get the id of newsletter using "params[:newsletter_id]", but in case of "document_download" action, you are using "params[:id]". I suspect that is the reason behind the issue. Try to use "params[:newsletter_id]" in document_download as well.

在“set_newsletter”方法中,您尝试使用“params [:newsletter_id]”来获取新闻稿的ID,但是在“document_download”操作的情况下,您使用的是“params [:id]”。我怀疑这就是问题背后的原因。尝试在document_download中使用“params [:newsletter_id]”。