我是RoR的新手,我在为现有数据库实现rails应用程序时遇到了一些问题。最后一个我无法摆脱,我在网上也找不到任何关于这个,所以我决定在这里发布。提前谢谢!
错误:
当我尝试将新寄存器插入站点表时,收到此错误:
ActiveRecord::StatementInvalid in SiteController#create
Mysql2::Error: Column 'idcustomer' cannot be null: INSERT INTO `site` (`name`, `address`, `postcode`, `idcustomer`, `observations`) VALUES (NULL, NULL, NULL, NULL, NULL)
Rails.root: /Users/biali/rails/gtonline
Application Trace | Framework Trace | Full Trace app/controllers/site_controller.rb:37:in `block in create' app/controllers/site_controller.rb:36:in `create'
请求
参数:
{"utf8"=>"✓",
"authenticity_token"=>"5Yzo/fIZ7SnRuEHHep6bpuQsRcwl0goLLDg=",
"site"=>{"name"=>"1321231",
"address"=>"32131",
"postcode"=>"321231",
"observations"=>"321321321"},
"idcustomer"=>"64", "commit"=>"Create
Site", "belongs_to"=>:customer}
表:
mysql> desc site;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| idsite | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | YES | | NULL | |
| address | text | YES | | NULL | |
| postcode | varchar(8) | YES | | NULL | |
| idcustomer | int(11) | NO | MUL | NULL | |
| observations | text | YES | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
6 rows in set (0.04 sec)
模特:
class Site < ActiveRecord::Base
set_table_name 'site'
set_primary_key 'idsite'
belongs_to :customer, :foreign_key=>'idcustomer'
end
控制器:
...
def new
@site = Site.new
respond_to do |format|
format.html
format.xml { render :xml => @site }
end
end
def create
@site = Site.new(params[:id])
respond_to do |format|
if @site.save
format.html { redirect_to(@site, :notice => 'Site was successfully added.') }
format.xml { render :xml => @site, :status => :created, :location => @site }
else
format.html { render :action => "new" }
format.xml { render :xml => @site.errors, :status => :unprocessable_entity }
end
end
end
...
观点:
<h1>New Site</h1>
<%= render 'form' %>
<%= link_to 'Back', sites_path %>
_form:
<%= form_for(@site) do |f| %>
<% if @site.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@site.errors.count, "error") %> prohibited this insertion from being saved:</h2>
<ul>
<% @site.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_area :address %>
</div>
<div class="field">
<%= f.label :postcode %><br />
<%= f.text_field :postcode %>
</div>
<div class="field">
<%= f.label :observations %><br />
<%= f.text_area :observations %>
</div>
<div class="field">
Customer<br />
<%= select_tag(:idcustomer, options_for_select(Customer.order('name').map {|customer| [customer.name, customer.idcustomer]}, @idcustomer)) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
答案 0 :(得分:0)
<%= select_tag(:idcustomer, options_for_select(Customer.order('name').map {|customer| [customer.name, customer.idcustomer]}, @idcustomer)) %>
应该是这样的:
<%= f.select :idcustomer, options_for_select(Customer.order('name').map{|customer| [customer.name, customer.idcustomer]}, @idcustomer) %>
或
<%= select_tag('site[idcustomer]', options_for_select(Customer.order('name').map {|customer| [customer.name, customer.idcustomer]}, @idcustomer)) %>