Ruby on rails Rails 4未将值插入数据库

Ruby on rails Rails 4未将值插入数据库,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我正在开发一个小的演示应用程序来帮助自己理解如何在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:

我正在开发一个小的演示应用程序来帮助自己理解如何在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
型号:

app/views/prop_sub_types/_form.html.erb:

我正在修改一些Rails3代码,所以我认为我弄乱了强参数部分,但在我看来,这些代码实际上是正确的,并且与一个不相关但正常工作的Rails4应用程序相比,效果很好


我做错了什么使插入忽略我的参数?

我相信这是因为模型中的attr\u访问器:name,:prop\u type\u id。这些访问器肯定是冗余的,并且覆盖了正确的访问器:DB表字段的所有字段都是作为初始化AR:Base class的一部分创建的。

您的表单看起来像什么?@ihaztehcodez View代码添加。OP可能会将attr_访问器与旧attr混淆_accessible@numbers1311407 exactly@RoaringStones哦天哪,猴子们!ActiveRecord是否透明地包含getter和setter?我将这个失败的应用程序与一个正在运行的应用程序进行了比较,在这个应用程序中,我不仅为几个虚拟属性,而且为每个字段都包含attr_accessor real db fields,但它仍然有效。我很想知道为什么会这样,但也许这是另外一个问题。@MarsAtomic是的。。。如果这是一个棘手的问题,我在一些技术采访中被问到,我会认为attr_accessor不会对工作产生太大影响:在所有方面,我相信Rails都将这些作为实例变量。但在Rails4中,我认为这些还不止这些。求你了,如果你找到了真正的答案,请在这里贴一个链接并注意我:@rowlingstones现在我知道我的问题出在哪里了,我有一个明确的目标。先看看这个。随着我对ActiveRecord的进一步研究,我将看看以后还能找到什么。
class PropSubType < ActiveRecord::Base
  attr_accessor :name, :prop_type_id

  belongs_to :prop_type
  has_many :appraisals
end
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
<%= 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 %>