2011-03-08 110 views
1

我是RoR的新手,我在爲現有數據庫實現rails應用程序時遇到了一些問題。我無法擺脫的最後一個,我也無法在網絡上找到任何關於此的內容,所以我決定在這裏發佈它。先謝謝你!SiteController中的ActiveRecord :: StatementInvalid#create 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
<%= 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)) %> 
相關問題