在我的 Rails 项目中,一切都运行良好,除了一件事。我正在尝试使用 check_box 编辑布尔值。但是当我去编辑时,无论 check_box 是否被选中,布尔值都保持为假。这是我的相关代码:
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
我知道我可能遗漏了一小段代码。任何帮助,将不胜感激!