【问题标题】:How to redirect to an existing record in the controller in Ruby on Rails如何在 Ruby on Rails 中重定向到控制器中的现有记录
【发布时间】: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


【解决方案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

【讨论】:

  • 干得好@metahamza!这就是我的意思!欲了解更多信息guides.rubyonrails.org/association_basics.html
  • 非常感谢大家!当我解决一个与将用户模型与简历相关联的新问题时,我将尝试实现它。我在创建操作时收到无方法错误。
猜你喜欢
  • 1970-01-01
  • 2011-12-11
  • 2016-05-20
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 2016-04-28
  • 2015-05-03
  • 1970-01-01
相关资源
最近更新 更多