我试图从模型中选择一个不同的列,然后在选择下拉列表中显示这些结果。除了未显示该列的值之外,其他所有内容似乎都正常运行。它显示某种十六进制值或其他内容。这是我的变量:
@incidenttypes = Incident.select(:incidenttype).distinct.order("incidenttype")
这是选择的形式:
<%= f.select :incidenttype, @incidenttypes, {include_blank: true}, class: 'form-control' %>
输出看起来像这样:#<Incident:0x0000000e97a88>
对我在做什么错有任何想法吗?
更新:这是表的架构:
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
答案 0 :(得分:3)
要了解更多options_for_select和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)
在optoptions_for_select中(选项和值的数组,提交表单后的slected_value)
<%= f.select :incidenttype, options_for_select(@incidenttypes, params[:incident][:incidenttype]), {include_blank: true}, class: 'form-control' %>