【问题标题】:Record Not Found rails 5 ActiveRecord Error找不到记录rails 5 ActiveRecord错误
【发布时间】:2016-07-07 21:41:55
【问题描述】:

我正在尝试使用一个虚 URL,该 URL 使用 SecureRandom 颁发的 ident 到涵洞,然后通过显示页面显示该涵洞。

这是错误信息的截图:

这是浏览器网址的截图:

我的涵洞控制器是:

我都试过了:

@culvert = Culvert.find_by_culvert_ident(params[:id])
  AND
@culvert = Culvert.find_by_id(params[:culvert_ident])

在我的涵洞控制器显示动作中,两者产生相同的结果(截图)

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_culvert
      @culvert = Culvert.find_by_culvert_ident(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def culvert_params
      params.require(:culvert).permit(:culvert_ident, :latitude, :longitude, :address, :user_id)
    end

这是我的涵洞模型标识生成器和虚 url 方法:

  before_create :generate_culvert_ident

  # Relationships
  belongs_to :user

  # Model Validations
  validates_uniqueness_of :culvert_ident

  # Ident Generator
  def generate_culvert_ident
    begin
      self.culvert_ident = SecureRandom.hex(3).upcase
      other_culvert = Culvert.find_by(culvert_ident: self.culvert_ident)
    end while other_culvert
  end

  # Url Direction
  def to_param
    culvert_ident
  end

所以我的目标是创建涵洞,自动分配唯一标识符,保存并使用自定义标识符显示涵洞,而不是标准的 1,2,3,4 id's

这适用于我使用过的另一个网络应用程序,设置完全相同,但我在这里遇到此错误,无法弄清楚原因。如果您需要更多信息,请告诉我!

**

EDIT #1 - 添加控制台输出的屏幕截图

**

【问题讨论】:

  • 记录是否与id一起存在?
  • 是的,当我在控制台中搜索它时,它会提取记录并显示 ident 以及标准 id 已保存 - 实际上它显示为 nil - 请参见上面的屏幕截图,现在就更新它
  • Culvert.where('culvert_ident = ?', 'BF83D4').first 提供了什么?
  • 它不会在控制台中返回 nil 生病添加上面的控制台输出 - 它似乎可以与您的查询一起使用它格式化为 wonkey 其原始上方的小屏幕截图
  • 我相信find_by_column_name 不再起作用了。我相信 RoR 4 中的语法已经改变 - 尝试使用 find_by(:culvert_ident: 'BF83D4')。但是,使用wherefirst 可以获得相同的结果。

标签: ruby ruby-on-rails-4 rails-activerecord


【解决方案1】:

所以这里的问题是我移除了涵洞控制器

before_action :set_culvert

一旦我重新添加了 set_user 操作,问题就解决了。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多