2017-08-24 62 views
0

我有我的應用程序中的簡歷控制器和歡迎控制器。 welcomes控制器只有一個用於根頁面的索引操作。簡歷控制器的目的是作爲登錄用戶上傳(新建/創建),下載(下載)等PDF文件,並且迄今爲止效果很好。如何訪問一個變量從一個不同的控制器,使自定義下載操作工作

我想要在根頁面上實現恢復下載功能(welcomes_controller/index)。

我該如何做到這一點? 因爲我無法調用變量來訪問welcomes控制器中的簡歷模型。路線應該如何?我應該在welcomes_controller上修改什麼?

的routes.rb

Rails.application.routes.draw do 

    devise_for :users 

    root 'welcomes#index' 

    resources :resumes do 
    get :download, on: :member 
    end 

    get '*path' => redirect('/') 
end 

resumes_controller.rb

class ResumesController < ApplicationController 
    around_filter :catch_not_found 
    before_action :find_resume, only: [ :show, :edit, :update, :destroy, :download ] 
    before_action :authenticate_user! 

    def show 
    end 

    def new 
    if @resume = current_user.resume 
     redirect_to @resume 
    else 
     @resume = Resume.new 
    end 
    end 

    def create 
    @resume = current_user.build_resume(resume_params) 
    if @resume.save 
     redirect_to @resume 
    else 
     render :new 
    end 
    end 

    def edit 
    end 

    def update 
    if @resume.update resume_params 
     redirect_to @resume, notice: "Your resume was successfully saved!" 
    else 
     render 'edit' 
    end 
    end 

    def destroy 
    @resume.destroy 
    redirect_to new_resume_path, notice: "Your resume was successfully deleted!" 
    end 

    def download 
    send_data @resume, type: "application/pdf", disposition: "attachment" 
    end 

    private 

    def resume_params 
    params.require(:resume).permit(:user_id, :download_file, :remove_download_file) 
    end 

    def find_resume 
    @resume = Resume.find(params[:id]) 
    end 

    def catch_not_found 
    yield 
    rescue ActiveRecord::RecordNotFound 
    redirect_to(root_url, :notice => 'Record not found') 
    end 

end 

簡歷/ show.html.erb

... 
... 
<%= link_to "Download", download_resume_path(@resume), "data-turbolinks" => false %> 

welcomes_controller.rb

class WelcomesController < ApplicationController 

    def index 
    end 

end 

回答

1

你可以從歡迎控制器訪問恢復模式,雖然控制器歡迎您可以撥打簡歷模板見下文

class WelcomesController < ApplicationController 

    def index 
    # you can call Resume model frome here 
    @resumes = Resume.all 
    end 

    def show 
    @resume = Resume.find(params[:id]) 
    end 

end 
樣本
相關問題