【发布时间】:2017-08-22 20:11:54
【问题描述】:
我正在应用程序中构建一项功能,用户可以使用carrierwave 将简历上传为“pdf”。应该只能上传一个文件,但我不能使记录在数据库级别上唯一,也不能使用唯一性作为模型文件的验证,因为carrierwave 不允许这样做。
为了解决这个问题,我决定如果存在任何记录,我至少要从新操作重定向到显示操作(现有“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 关联吗?这样每个用户只能关联一个简历模型。
标签: ruby-on-rails activerecord model carrierwave