rails 表单 select 显示结果为十六进制的问题
Issue with rails form select displaying results as hex
我正在尝试 select 来自模型的不同列,然后在 select 下拉列表中显示这些结果。除了不显示列的值外,一切似乎都在工作。它显示某种十六进制值或其他东西。这是我的变量:
@incidenttypes = Incident.select(:incidenttype).distinct.order("incidenttype")
这是表格 select:
<%= f.select :incidenttype, @incidenttypes, {include_blank: true}, class: 'form-control' %>
输出如下所示:#<Incident:0x0000000e97a88>
对我做错了什么有什么想法吗?
更新:这是 table 的架构:
create_table "incidents", force: :cascade do |t|
t.string "street"
t.string "city"
t.string "zip"
t.integer "dayofweek"
t.date "timeofday"
t.string "state"
t.string "incidenttype"
t.string "drnum"
t.string "weatherevent"
t.string "specialevent"
t.float "latitude"
t.float "longitude"
t.text "comments"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
这是搜索表单:
<%= form_for(:search, url: incidents_path, method: :get) do |f| %>
<div class="ui-bordered px-4 pt-4 mb-4">
<div class="form-row">
<div class="col-md mb-4">
<label class="form-label">Type</label>
<%= f.select :dayofweek, Incident.dayofweeks.keys, {:include_blank=> 'Any days', :selected => @dayofweek}, class: 'custom-select' %>
</div>
<div class="col-md mb-4">
<label class="form-label">Incident Type</label>
<%= f.select :incidenttype, options_for_select(@incidenttypes), {include_blank: true}, class: 'form-control' %>
</div>
<div class="col-md mb-4">
<label class="form-label">Created date</label>
<%= f.text_field :created_at, class: 'form-control', id: 'tickets-list-created', :autocomplete => :off, value: created_at_from_parameters %>
</div>
<div class="col-md col-xl-2 mb-4">
<label class="form-label d-none d-md-block"> </label>
<button type="submit" class="btn btn-secondary btn-block">Show</button>
</div>
</div>
</div>
<% end %>
事件控制者:
class IncidentsController < ApplicationController
before_action :set_incident, only: [:show, :edit, :update, :destroy]
# GET /incidents
# GET /incidents.json
def index
if params[:search] && params[:search][:created_at].present?
start_date, end_date = params[:search][:created_at].split(' - ')
@incidents = Incident.where(created_at: start_date..end_date).where(dayofweek: params[:search][:dayofweek]).where(incidenttype: params[:search][:incidenttype])
@dayofweek = params[:search][:dayofweek]
@incidenttypes = Incident.order("incidenttype").pluck(:incidenttype, :incidenttype).uniq
else
@incidents = Incident.all
end
end
def map
@incidents = Incident.all
end
# GET /incidents/1
# GET /incidents/1.json
def show
end
# GET /incidents/new
def new
@incident = Incident.new
end
# GET /incidents/1/edit
def edit
end
# POST /incidents
# POST /incidents.json
def create
@incident = Incident.new(incident_params)
respond_to do |format|
if @incident.save
format.html { redirect_to @incident, notice: 'Incident was successfully created.' }
format.json { render :show, status: :created, location: @incident }
else
format.html { render :new }
format.json { render json: @incident.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /incidents/1
# PATCH/PUT /incidents/1.json
def update
respond_to do |format|
if @incident.update(incident_params)
format.html { redirect_to @incident, notice: 'Incident was successfully updated.' }
format.json { render :show, status: :ok, location: @incident }
else
format.html { render :edit }
format.json { render json: @incident.errors, status: :unprocessable_entity }
end
end
end
# DELETE /incidents/1
# DELETE /incidents/1.json
def destroy
@incident.destroy
respond_to do |format|
format.html { redirect_to incidents_url, notice: 'Incident was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_incident
@incident = Incident.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def incident_params
params.require(:incident).permit(:street, :city, :zip, :dayofweek, :timeofday, :state, :incidenttype, :drnum, :weatherevent, :specialevent, :latitude, :longitude, :comments)
end
end
了解更多options_for_select and Select helper
将您的查询重构为
@incidenttypes = Incident.order("incidenttype").distinct.pluck(:incidenttype, :id)
使用options_for_select(@incidenttypes)
<%= f.select :incidenttype, options_for_select(@incidenttypes), {include_blank: true}, class: 'form-control' %>
使用形式如下
@incidenttypes = Incident.order("incidenttype").distinct.pluck(:incidenttype, :incidenttype)
in optoptions_for_select(选项和值数组的数组,提交表单后slected_value)
<%= f.select :incidenttype, options_for_select(@incidenttypes, params[:incident][:incidenttype]), {include_blank: true}, class: 'form-control' %>
我正在尝试 select 来自模型的不同列,然后在 select 下拉列表中显示这些结果。除了不显示列的值外,一切似乎都在工作。它显示某种十六进制值或其他东西。这是我的变量:
@incidenttypes = Incident.select(:incidenttype).distinct.order("incidenttype")
这是表格 select:
<%= f.select :incidenttype, @incidenttypes, {include_blank: true}, class: 'form-control' %>
输出如下所示:#<Incident:0x0000000e97a88>
对我做错了什么有什么想法吗?
更新:这是 table 的架构:
create_table "incidents", force: :cascade do |t|
t.string "street"
t.string "city"
t.string "zip"
t.integer "dayofweek"
t.date "timeofday"
t.string "state"
t.string "incidenttype"
t.string "drnum"
t.string "weatherevent"
t.string "specialevent"
t.float "latitude"
t.float "longitude"
t.text "comments"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
这是搜索表单:
<%= form_for(:search, url: incidents_path, method: :get) do |f| %>
<div class="ui-bordered px-4 pt-4 mb-4">
<div class="form-row">
<div class="col-md mb-4">
<label class="form-label">Type</label>
<%= f.select :dayofweek, Incident.dayofweeks.keys, {:include_blank=> 'Any days', :selected => @dayofweek}, class: 'custom-select' %>
</div>
<div class="col-md mb-4">
<label class="form-label">Incident Type</label>
<%= f.select :incidenttype, options_for_select(@incidenttypes), {include_blank: true}, class: 'form-control' %>
</div>
<div class="col-md mb-4">
<label class="form-label">Created date</label>
<%= f.text_field :created_at, class: 'form-control', id: 'tickets-list-created', :autocomplete => :off, value: created_at_from_parameters %>
</div>
<div class="col-md col-xl-2 mb-4">
<label class="form-label d-none d-md-block"> </label>
<button type="submit" class="btn btn-secondary btn-block">Show</button>
</div>
</div>
</div>
<% end %>
事件控制者:
class IncidentsController < ApplicationController
before_action :set_incident, only: [:show, :edit, :update, :destroy]
# GET /incidents
# GET /incidents.json
def index
if params[:search] && params[:search][:created_at].present?
start_date, end_date = params[:search][:created_at].split(' - ')
@incidents = Incident.where(created_at: start_date..end_date).where(dayofweek: params[:search][:dayofweek]).where(incidenttype: params[:search][:incidenttype])
@dayofweek = params[:search][:dayofweek]
@incidenttypes = Incident.order("incidenttype").pluck(:incidenttype, :incidenttype).uniq
else
@incidents = Incident.all
end
end
def map
@incidents = Incident.all
end
# GET /incidents/1
# GET /incidents/1.json
def show
end
# GET /incidents/new
def new
@incident = Incident.new
end
# GET /incidents/1/edit
def edit
end
# POST /incidents
# POST /incidents.json
def create
@incident = Incident.new(incident_params)
respond_to do |format|
if @incident.save
format.html { redirect_to @incident, notice: 'Incident was successfully created.' }
format.json { render :show, status: :created, location: @incident }
else
format.html { render :new }
format.json { render json: @incident.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /incidents/1
# PATCH/PUT /incidents/1.json
def update
respond_to do |format|
if @incident.update(incident_params)
format.html { redirect_to @incident, notice: 'Incident was successfully updated.' }
format.json { render :show, status: :ok, location: @incident }
else
format.html { render :edit }
format.json { render json: @incident.errors, status: :unprocessable_entity }
end
end
end
# DELETE /incidents/1
# DELETE /incidents/1.json
def destroy
@incident.destroy
respond_to do |format|
format.html { redirect_to incidents_url, notice: 'Incident was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_incident
@incident = Incident.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def incident_params
params.require(:incident).permit(:street, :city, :zip, :dayofweek, :timeofday, :state, :incidenttype, :drnum, :weatherevent, :specialevent, :latitude, :longitude, :comments)
end
end
了解更多options_for_select and Select helper
将您的查询重构为
@incidenttypes = Incident.order("incidenttype").distinct.pluck(:incidenttype, :id)
使用options_for_select(@incidenttypes)
<%= f.select :incidenttype, options_for_select(@incidenttypes), {include_blank: true}, class: 'form-control' %>
使用形式如下
@incidenttypes = Incident.order("incidenttype").distinct.pluck(:incidenttype, :incidenttype)
in optoptions_for_select(选项和值数组的数组,提交表单后slected_value)
<%= f.select :incidenttype, options_for_select(@incidenttypes, params[:incident][:incidenttype]), {include_blank: true}, class: 'form-control' %>