2017-08-22 34 views
1

我在應用程序中構建了一個功能,用戶可以使用carrierwave將簡歷上載爲「pdf」。應該只能上傳一個文件,但是我不能在數據庫級別創建唯一的記錄,也不能在模型文件中使用unique作爲驗證,因爲carrierwave不允許它。如何在Ruby on Rails中將控件重定向到控制器中

爲了解決這個問題,我決定至少要將新動作重定向到show動作(現有的「download_file」),如果有任何記錄存在,那麼用戶將無法將第二個文件上傳到數據庫。

我想我的問題是,新行動發現一個ID爲1的銷燬的簡歷記錄,這就是爲什麼表單不會呈現,我不會被重定向到現有的記錄。

我該如何做到這一點?我錯過了什麼?

resumes_contrller.rb

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

    def show 
    end 

    def new 
    if Resume.exists? 
     redirect_to Resume.find(params[:id]) 
    else 
     @resume = Resume.new 
    end 
    end 

    def create 
    @resume = Resume.new 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 

    private 

    def resume_params 
    params.require(:resume).permit(: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 

schema.rb

ActiveRecord::Schema.define(version: 20170821213418) do 

    create_table "resumes", force: :cascade do |t| 
    t.string "download_file" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "users", force: :cascade do |t| 
    t.string "email",     default: "", null: false 
    t.string "encrypted_password",  default: "", null: false 
    t.string "reset_password_token" 
    t.datetime "reset_password_sent_at" 
    t.datetime "remember_created_at" 
    t.integer "sign_in_count",   default: 0, null: false 
    t.datetime "current_sign_in_at" 
    t.datetime "last_sign_in_at" 
    t.string "current_sign_in_ip" 
    t.string "last_sign_in_ip" 
    t.datetime "created_at",       null: false 
    t.datetime "updated_at",       null: false 
    end 

    add_index "users", ["email"], name: "index_users_on_email", unique: true 
    add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 

end 

development.log

Started GET "/resumes/new" for 77.8.47.62 at 2017-08-22 19:58:07 +0000 
Cannot render console from 77.8.47.62! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 
Processing by ResumesController#new as HTML 
    [1m[36mResume Exists (0.1ms)[0m [1mSELECT 1 AS one FROM "resumes" LIMIT 1[0m 
    [1m[35mResume Load (0.1ms)[0m SELECT "resumes".* FROM "resumes" WHERE "resumes"."id" = ? LIMIT 1 [["id", nil]] 
Redirected to https://rails-tutorial-martinbortowski.c9.io/ 
Completed 302 Found in 10ms (ActiveRecord: 0.9ms) 


Started GET "/" for 77.8.47.62 at 2017-08-22 19:58:08 +0000 
Cannot render console from 77.8.47.62! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 
Processing by WelcomesController#index as HTML 
    Rendered welcomes/index.html.erb within layouts/application (0.3ms) 
Completed 200 OK in 59ms (Views: 58.2ms | ActiveRecord: 0.0ms) 
+1

可你只需要使用簡歷和用戶模型之間的關聯HAS_ONE?這樣每個用戶只能有一個關聯的簡歷模型。 –

回答

1

在您的代碼中,Resume.exists?類似於提問「我的數據庫中是否至少有一個簡歷記錄?」

如果你檢查它產生的查詢,你將看到類似

SELECT 1 AS one FROM `resumes` LIMIT 1

我相信你真正想要做的是簡歷關聯到用戶。如Pedro所述,您需要在UserResume之間建立關係以跟蹤關聯。

然後你就會想是

class ResumesController < ApplicationController 
    # ... 

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

    # ... 
end 
+2

做得好@metahamza!這就是我的意思!欲瞭解更多信息http://guides.rubyonrails.org/association_basics.html –

+0

非常感謝你們!當我解決一個新的問題時,我會嘗試實現它......將用戶模型與簡歷關聯起來。我在創建操作中遇到了無方法錯誤。 – trickydiddy

+1

非常感謝! ;) – trickydiddy

相關問題