我在應用程序中構建了一個功能,用戶可以使用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)
可你只需要使用簡歷和用戶模型之間的關聯HAS_ONE?這樣每個用戶只能有一個關聯的簡歷模型。 –