除了一件事,我的rails项目中的一切都运行良好。我正在尝试用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
我知道我可能错过了一小段代码。任何帮助将不胜感激!
答案 0 :(得分:1)
我认为问题在于:
add_column :subnets, :multicast, :boolean, :default => 0
default => 0
你的意思是什么?
布尔值可以得到两个值:true
和false
... 0
和false
不一样......
您应该删除:default => 0
或使用以下内容:
:multicast, :boolean, :default => false, :null => false