选定数据(静态选择框)在编辑视图中不显示

时间:2015-11-11 02:59:58

标签: html css ruby postgresql ruby-on-rails-4

我的编辑视图中的选择框出现问题(Rails 4 Ruby 2.2)App。

选择框如下所示:

 <%= f.select(:primary_type, options_for_select([['RESIDENT - COMPLAINT', 

'RESIDENT - COMPLAINT'], ['THEFT', 'THEFT'], ['PROPERTY DAMAGE', 'PROPERTY 

DAMAGE'], ['DOORS / WINDOWS BROKEN', 'DOORS / WINDOWS BROKEN'], ['WATER LEAKING', 

'WATER LEAKING'], ['FLOODING', 'FLOODING'],['ACCIDENT - PERSONAL INJURY', 

'ACCIDENT - PERSONAL INJURY'], ['ACCIDENT - VEHICLE', 'ACCIDENT - VEHICLE'], 

['ACCIDENT - OTHER', 'ACCIDENT - OTHER'], ['FENCES / GATES BROKEN', 'FENCES / 

GATES BROKEN'], ['SUSPICIOUS ACTIVITY - PERSON', 'SUSPICIOUS ACTIVITY - PERSON'], 

['SUSPICIOUS ACTIVITY - VEHICLE', 'SUSPICIOUS ACTIVITY - VEHICLE'], ['VANDALISM - 

REPORTING', 'VANDALISM - REPORTING'], ['VANDALISM - IN PROGRESS', 'VANDALISM - IN 

PROGRESS'], ['HAZZARD REPORT - SAFETY', 'HAZZARD REPORT - SAFETY'], ['HAZZARD 

REPORT - FIRE', 'HAZZARD REPORT - FIRE'], ['FIRE - ACTIVE', 'FIRE - ACTIVE'], 

['PARKING COMPLAINT - RESIDENT', 'PARKING COMPLAINT - RESIDENT'], ['PARKING 

COMPLAINT - VISITOR', 'PARKING COMPLAINT - VISITOR'], ['BREAK AND ENTER - 

ATTEMPT', 'BREAK AND ENTER - ATTEMPT'], ['BREAK AND ENTER - CONFIRMED', 'BREAK AND 

ENTER - CONFIRMED'], ['ALARM PANEL - PROBLEMS', ' ALARM PANEL - PROBLEMS'], ['GAS 

LEAK', 'GAS LEAK'], ['EXPLOSION', 'EXPLOSION'], ['DAMAGE - WEATHER RELATED', 

'DAMAGE - WEATHER RELATED']]), { include_blank: true }, class: 'input-group input-

group-lg', id: 'primary-box')%>

在最初选择时保存到数据库,并通过Rails控制台确认了这一点,但在我转到编辑页面时不知道如何将其显示在选择框中作为主要值。

我的控制器如下:

class CallsController < ApplicationController
  before_action :set_call, only: [:show, :edit, :update, :destroy]

  # GET /calls
  # GET /calls.json
  def index
    @calls = Call.all
    @active_calls = @calls.select{|x| x.status == 'ACTIVE'}
    @pending_calls = @calls.select{|x| x.status == 'PENDING'}
  end

  # GET /calls/1
  # GET /calls/1.json
  def show
  end

  # GET /calls/new
  def new
    @call = Call.new
  end

  # GET /calls/1/edit
  def edit
  end

  # POST /calls
  # POST /calls.json
  def create
    @call = Call.new(call_params)

    respond_to do |format|
      if @call.save
        format.html { redirect_to @call, notice: 'Call was successfully created.' }
        format.json { render :show, status: :created, location: @call }
      else
        format.html { render :new }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /calls/1
  # PATCH/PUT /calls/1.json
  def update
    respond_to do |format|
      if @call.update(call_params)
        format.html { redirect_to @call, notice: 'Call was successfully updated.' }
        format.json { render :show, status: :ok, location: @call }
      else
        format.html { render :edit }
        format.json { render json: @call.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /calls/1
  # DELETE /calls/1.json
  def destroy
    @call.destroy
    respond_to do |format|
      format.html { redirect_to calls_url, notice: 'Call was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_call
      @call = Call.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def call_params
      params.require(:call).permit(:call_time, :status, :primary_type, :secondary_type, :site, :address, :unit_1, :unit_2, :unit_3, :unit_4, :call_details, :unit_on_scene, :unit_clear, :call_num, :site_id)
    end
end

任何帮助将不胜感激!在此先感谢!如果需要更多信息,请告诉我并将其放在这里。

2 个答案:

答案 0 :(得分:0)

如果您询问如何设置默认设置,则会按照rails api docs

中的答案进行操作

例如:

<%= f.select(:primary_type,  options_for_select([["Text1", "value1"],["Text2","value2"] "value2") %>

编辑:

在编辑页面上,您应该在控制器中设置一个变量集,例如@call = Call.find(params[:id])

然后在视图中你可以这样做:

<%= f.select(:primary_type,  options_for_select([<your_values>], @call.primary_type ) %>

答案 1 :(得分:0)

好的,所以在阅读了一些其他帖子并深入探讨后,我找到了解决方案,它似乎工作得非常好。

<%= f.select(:primary_type, options_for_select([['Opt 1', 'Opt 1'],['Opt 2', 'Opt 2']], **:selected => @call.primary_type** )) %>

检索并显示Selected值的调用位于** **之间。不要在代码中添加星号..