gpt4 book ai didi

mysql - SiteController 中的 ActiveRecord::StatementInvalid#create Rails 为所有字段插入空值

转载 作者:行者123 更新时间:2023-11-30 23:38:45 25 4
gpt4 key购买 nike

我是 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

Controller :

...

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

...

View :

<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 %>

最佳答案

<%= 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)) %>

关于mysql - SiteController 中的 ActiveRecord::StatementInvalid#create Rails 为所有字段插入空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5238858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com