-2

Railsプロジェクトでは、1つを除いてすべてが正常に機能しています。check_box でブール値を編集できるようにしようとしています。しかし、編集に行くと、check_box がチェックされているかどうかにかかわらず、ブール値は false のままです。これが私の関連コードです:

class SubnetsController < ApplicationController
  # GET /subnets
  # GET /subnets.json
  def index
    @subnets = Subnet.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render :json => @subnets }
    end
  end

  def normal
    @subnets = Subnet.all

    respond_to do |format|
      format.html # normal.html.erb
      format.json { render :json => @subnets }
    end
  end

  # GET /subnets/1
  # GET /subnets/1.json
  def show
    @subnet = Subnet.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @subnet }
    end
  end

  # GET /subnets/new
  # GET /subnets/new.json
  def new
    @subnet = Subnet.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @subnet }
    end
  end

  # GET /subnets/1/edit
  def edit
    @subnet = Subnet.find(params[:id])
  end

  # POST /subnets
  # POST /subnets.json
  def create
    @subnet = Subnet.new(params[:subnet])

    respond_to do |format|
      if @subnet.save
        format.html { redirect_to @subnet, :notice => 'Subnet was successfully created.' }
        format.json { render :json => @subnet, :status => :created, :location => @subnet }
      else
        format.html { render :action => "new" }
        format.json { render :json => @subnet.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /subnets/1
  # PUT /subnets/1.json
  def update
    @subnet = Subnet.find(params[:id])

    respond_to do |format|
      if @subnet.update_attributes(params[:subnet])
        format.html { redirect_to @subnet, :notice => 'Subnet was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :action => "edit" }
        format.json { render :json => @subnet.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /subnets/1
  # DELETE /subnets/1.json
  def destroy
    @subnet = Subnet.find(params[:id])
    @subnet.destroy

    respond_to do |format|
      format.html { redirect_to subnets_url }
      format.json { head :no_content }
    end
  end
end

意見:

<%= form_for(@subnet) do |f| %>
  <% if @subnet.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@subnet.errors.count, "error") %> prohibited this subnet from being saved:</h2>

      <ul>
      <% @subnet.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :SubnetName %><br />
    <%= f.text_field :SubnetName %>
  </div>
  <div class="field">
    <%= f.label :FW_Context_BridgeGroup %><br />
    <%= f.number_field :FW_Context_BridgeGroup %>
  </div>
  <div class="field">
    <%= f.label :VlanNumber %><br />
    <%= f.number_field :VlanNumber %>
  </div>
  <div class="field">
    <%= f.label :FwVlan %><br />
    <%= f.number_field :FwVlan %>
  </div>
  <div class="field">
    <%= f.label :NetAddress %><br />
    <%= f.text_field :NetAddress %>
  </div>
  <div class="field">
    <%= f.label :DefaultRouter %><br />
    <%= f.text_field :DefaultRouter %>
  </div>
  <div class="field">
    <%= f.label :Netmask %><br />
    <%= f.text_field :Netmask %>
  </div>
  <div class="field">
    <%= f.label :Broadcast %><br />
    <%= f.text_field :Broadcast %>
  </div>
  <div class="field">
    <%= f.label :Notes %><br />
    <%= f.text_field :Notes %>
  </div>
  **<div class="field">
    <%= f.label :multicast %><br />
    <%= f.check_box :multicast %>
  </div>**
  <div class="actions">
    <%= f.submit %>
<% end %>

移行:

class AddMulticast < ActiveRecord::Migration
  def up
    add_column :subnets, :multicast, :boolean, :default => 0
  end

  def down
    remove_column :subnets, :multicast
  end

end

私はおそらくいくつかの小さなコードが欠けていることを知っています。どんな助けでも大歓迎です!

4

1 に答える 1