如何解决未初始化的常量搜索::错误

AJN

我正在为与汽车维护相关的约会模型创建一个高级搜索/过滤器,其中每个 schema.rb 中的表是:

  create_table "appointments", force: :cascade do |t|
    t.string "VIN"
    t.string "owner_email"
    t.string "date"
    t.string "time"
    t.string "reason"
    t.string "parts_needed"
    t.string "hours_needed"
    t.string "cost"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "searches", force: :cascade do |t|
    t.string "VIN"
    t.string "email"
    t.string "after_date"
    t.string "before_date"
    t.string "time"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

在我的 search.rb 模型中,我定义了搜索功能:

class Search < ApplicationRecord

    def search_appointments
        appointments = Appointment.all
        # appointments = appointments.where("VIN LIKE ?", VIN) if VIN.present? GIVES ERROR
        appointments = appointments.where("owner_email LIKE ?", email) if email.present?
        appointments = appointments.where("date >= ?", after_date) if after_date.present?
        appointments = appointments.where("date <= ?", before_date) if before_date.present?
        if !time=="Any"
            appointments = appointments.where("time LIKE ?", time) if time.present?
        end

        return appointments
    end
end

然后在我的 show.html.erb 中显示结果过滤器:

<h2 align="center">Search Results</h2>
</br>


<div class="container-fluid">
    <% if @search.search_appointments.empty? %>
        <p> No Appointments Fit This Search</p>

    <% else %>
        <%= @search.search_appointments.each do |a| %>

        Email: <%= a.owner_email%> </br>
        Date: <%= a.date%> </br>
        Time: <%= a.time%> </br>
        VIN: <%= a.VIN %> </br>

        </br>
        </br>
        </br>
        <% end %>
    <% end %>
    </br>
    <%= link_to 'Return', @search, method: :delete %>
</div>

一切正常,除了 search.rb 模型中的第一次过滤尝试,该行被注释掉。如果我取消注释该行并运行搜索,则会突出显示该行并出现错误:

uninitialized constant Search::VIN

我不明白为什么会发生这种情况,因为所有其他过滤器都可以正常工作。我将不胜感激任何建议,谢谢。

搜索控制器:

class SearchesController < ApplicationController

    def new
        @search = Search.new
    end

    def create
        @search = Search.create(search_params)
        redirect_to @search
    end

    def show
        @search = Search.find(params[:id])
    end

    def destroy
        @search = Search.find(params[:id])
        @search.destroy

        redirect_to admin_path
    end

    def search_params
        params.require(:search).permit(:VIN, :email, :after_date, :before_date, :time)
    end
end

我的“新”页面是一个表单,用户在其中填写过滤器参数,然后单击提交按钮将它们带到列出过滤约会的显示页面。

草甸

当您VINappointments.where("VIN LIKE ?", VIN)ruby 中引用正在寻找一个常量,因为它是大写的。为了访问您的属性,您需要self.VIN将列名引用或更改为小写(推荐)。

选项1: appointments = appointments.where("VIN LIKE ?", self.VIN) if self.VIN.present?

选项 2:

  • VIN更改vin
  • appointments = appointments.where("vin LIKE ?", vin) if vin.present?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我如何解决 TinyButStrong 中的“未初始化的字符串偏移:”错误

错误未解决的类型,如何初始化dbhelper?

如何解决此错误消息“初始化错误(Eslint)。意外的标识符”?

为什么收到错误“未初始化的常量SecureRandom?

Rails的路由错误-未初始化的常量SubscribersController

最小测试未初始化的常量错误

Rails-Sidekiq错误未初始化的常量

未初始化的常量 MiniTest::Test 错误

Rails未初始化的常量名称错误

Ruby错误-未初始化的常量OpenStruct(NameError)

错误:未初始化的常量Guard :: Haml :: Engine

MiniTest错误:“ NameError:未初始化的常量”

Ruby类中的未初始化常量错误

设计错误:NameError(未初始化的常量Unlock)

Net :: IMAP未初始化的常量错误

RSpec错误:未初始化的常量ModuleName :: Chef(NameError)

Rails:路由错误未初始化的常量 RegistrationsController

omniauth facebook未初始化常量错误

Rails显示错误“未初始化的常量URI :: Generic”

使用关系的未初始化常量错误

Ruby on Rails“未初始化的常量”错误

名称错误“未初始化的常量”ruby on rails

如何解决“有条件的跳跃或移动取决于未初始化的值” valgrind错误而导致的strlen错误?

如何修复“ NameError:未初始化的常量Thor :: Base”错误?

初始化Selenium WebDriver时如何解决python-Selenium错误“连接被拒绝”?

如何解决“正确初始化log4j系统”错误?

如何解决无法初始化设备hci0。错误?

如何解决错误:无法初始化主类选择。ClustererExecution

如何解决错误 C2440“正在初始化”:无法从“_Ty”转换为“_Objty”