Rails 4 没有将值插入数据库
Rails 4 not inserting values into database
我正在开发一个小型演示应用程序以帮助自己了解如何在 Rails 4 上实现级联选择,但我 运行 遇到了一个有趣的问题:
当我填写表单并将其提交到数据库时,参数似乎已传递,但值并未保存在数据库中,因为 SQL 跳过了字段名称和值,如在控制台的这段代码中进行了演示:
Started POST "/prop_sub_types" for ::1 at 2015-01-23 18:54:05 -0800
Processing by PropSubTypesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7mGqF/MR+IKrKKdAKBYBI/E7pl7PCJasHDLN5T1/ohF/qEuXyhwsm8d87xDvfmcxk590hp/2XuenQBDaGhI+IA==", "prop_sub_type"=>{"name"=>"sub foo", "prop_type_id"=>"1"}, "commit"=>"Create Prop sub type"}
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "prop_sub_types" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-01-24 02:54:05.218673"], ["updated_at", "2015-01-24 02:54:05.218673"]]
(2.2ms) commit transaction
Redirected to http://localhost:3000/prop_sub_types/1
Completed 302 Found in 7ms (ActiveRecord: 2.6ms)
这是 schema.rb 的相关位,表明列在那里:
create_table "prop_sub_types", force: :cascade do |t|
t.string "name"
t.integer "prop_type_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
型号:
class PropSubType < ActiveRecord::Base
attr_accessor :name, :prop_type_id
belongs_to :prop_type
has_many :appraisals
end
Controller(为了简洁起见,相关部分——如果需要,我可以 post 更多):
def create
@prop_sub_type = PropSubType.new(prop_sub_type_params)
respond_to do |format|
if @prop_sub_type.save
format.html { redirect_to @prop_sub_type, notice: 'Prop sub type was successfully created.' }
format.json { render :show, status: :created, location: @prop_sub_type }
else
format.html { render :new }
format.json { render json: @prop_sub_type.errors, status: :unprocessable_entity }
end
end
end
...
private
# Use callbacks to share common setup or constraints between actions.
def set_prop_sub_type
@prop_sub_type = PropSubType.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def prop_sub_type_params
params.require(:prop_sub_type).permit(:name, :prop_type_id)
end
app/views/prop_sub_types/_form.html.erb:
<%= form_for(@prop_sub_type) do |f| %>
<% if @prop_sub_type.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@prop_sub_type.errors.count, "error") %> prohibited this prop_sub_type from being saved:</h2>
<ul>
<% @prop_sub_type.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :prop_type_id %><br>
<%= f.number_field :prop_type_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我正在调整一些 Rails 3 代码,所以我认为我搞砸了强大的参数部分,但实际上代码对我来说看起来是正确的,并且与不相关但有效的 Rails 4个申请。
我做错了什么让 INSERT 忽略我的参数?
我相信这是因为您的 attr_accessor :name, :prop_type_id
模型。这些访问器肯定是多余的并且会覆盖正确的访问器:DB table 字段的所有字段都是作为初始化 AR:Base class
的一部分创建的
我正在开发一个小型演示应用程序以帮助自己了解如何在 Rails 4 上实现级联选择,但我 运行 遇到了一个有趣的问题:
当我填写表单并将其提交到数据库时,参数似乎已传递,但值并未保存在数据库中,因为 SQL 跳过了字段名称和值,如在控制台的这段代码中进行了演示:
Started POST "/prop_sub_types" for ::1 at 2015-01-23 18:54:05 -0800
Processing by PropSubTypesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7mGqF/MR+IKrKKdAKBYBI/E7pl7PCJasHDLN5T1/ohF/qEuXyhwsm8d87xDvfmcxk590hp/2XuenQBDaGhI+IA==", "prop_sub_type"=>{"name"=>"sub foo", "prop_type_id"=>"1"}, "commit"=>"Create Prop sub type"}
(0.1ms) begin transaction
SQL (0.3ms) INSERT INTO "prop_sub_types" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2015-01-24 02:54:05.218673"], ["updated_at", "2015-01-24 02:54:05.218673"]]
(2.2ms) commit transaction
Redirected to http://localhost:3000/prop_sub_types/1
Completed 302 Found in 7ms (ActiveRecord: 2.6ms)
这是 schema.rb 的相关位,表明列在那里:
create_table "prop_sub_types", force: :cascade do |t|
t.string "name"
t.integer "prop_type_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
型号:
class PropSubType < ActiveRecord::Base
attr_accessor :name, :prop_type_id
belongs_to :prop_type
has_many :appraisals
end
Controller(为了简洁起见,相关部分——如果需要,我可以 post 更多):
def create
@prop_sub_type = PropSubType.new(prop_sub_type_params)
respond_to do |format|
if @prop_sub_type.save
format.html { redirect_to @prop_sub_type, notice: 'Prop sub type was successfully created.' }
format.json { render :show, status: :created, location: @prop_sub_type }
else
format.html { render :new }
format.json { render json: @prop_sub_type.errors, status: :unprocessable_entity }
end
end
end
...
private
# Use callbacks to share common setup or constraints between actions.
def set_prop_sub_type
@prop_sub_type = PropSubType.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def prop_sub_type_params
params.require(:prop_sub_type).permit(:name, :prop_type_id)
end
app/views/prop_sub_types/_form.html.erb:
<%= form_for(@prop_sub_type) do |f| %>
<% if @prop_sub_type.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@prop_sub_type.errors.count, "error") %> prohibited this prop_sub_type from being saved:</h2>
<ul>
<% @prop_sub_type.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :prop_type_id %><br>
<%= f.number_field :prop_type_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我正在调整一些 Rails 3 代码,所以我认为我搞砸了强大的参数部分,但实际上代码对我来说看起来是正确的,并且与不相关但有效的 Rails 4个申请。
我做错了什么让 INSERT 忽略我的参数?
我相信这是因为您的 attr_accessor :name, :prop_type_id
模型。这些访问器肯定是多余的并且会覆盖正确的访问器:DB table 字段的所有字段都是作为初始化 AR:Base class