【问题标题】:No data is being save in `:building` column of rails database?没有数据保存在rails数据库的`:building`列中?
【发布时间】:2015-11-14 10:51:12
【问题描述】:

我创建了一个valuation 表,其中包含

class CreateValuations < ActiveRecord::Migration
  def change
    create_table :valuations do |t|
      t.text :location
      t.integer :number_of_bedroom
      t.integer :number_of_bath
      t.integer :age_of_building
      t.text :other_details
      t.string :name
      t.string :email
      t.integer :contact_number

      t.timestamps null: false
   end
 end

结束

后来我使用rails generate migration add_building_to_valuations building:text 在其中添加了一个类型为文本的列:building。我创建了一个表单

<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <%= form_for(@valuation) do |val| %>

            <%= val.label :building, "Name of Building/Builder" %>
            <%= val.text_field :building, class: 'form-control' %>

            <%= val.label :location, "Location" %>
            <%= val.text_field :location, class: 'form-control' %>

            <%= val.label :number_of_bedroom, "Number of Bedroom" %>
            <%= val.text_field :number_of_bedroom, class: 'form-control' %>

            <%= val.label :number_of_washroom, "Number of Washroom" %>
            <%= val.text_field :number_of_bath, class: 'form-control' %>

            <%= val.label :age_of_building, "Age of Building" %>
            <%= val.text_field :age_of_building, class: 'form-control' %>

            <%= val.label :name, "Name" %>
            <%= val.text_field :name, class: 'form-control' %>

            <%= val.label :email, "Email" %>
            <%= val.text_field :email, class: 'form-control' %>

            <%= val.label :contact_number, "Contact Number" %>
            <%= val.text_field :contact_number, class: 'form-control' %>

            <%= val.submit "Evaluate my property", class: "btn btn-primary" %>

        <% end %>
    </div>
</div>

当我填写所有字段并通过Evaluate my property 提交时,:building 中没有保存任何数据。

我使用以下命令检查了这一点 $ rails console $ Valuation.all

收到的输出跟随输出

#<Valuation id: 1, location: "Chandivali, Powai", 
number_of_bedroom: 3, number_of_bath: 3, 
age_of_building: 9, other_details: nil, name: "Shravan", 
email: "shravan.xxx@gmail.com", 
contact_number: "9876543210",created_at: "2015-11-14 09:47:26",
updated_at: "2015-11-14 09:47:26", building: nil>,

【问题讨论】:

  • 您是否有可能使用了强参数并且您没有更新许可以包含新的building 列?

标签: ruby-on-rails dbmigrate


【解决方案1】:

你应该更新你的valuation_params,这很可能是一个私有函数

private
    def valuation_params
        params.require(:valuation).permit(:building,
                 :location,
                 :number_of_bedroom, 
                 :number_of_bath, 
                 :age_of_building, 
                 :name, :email, :contact_number)
    end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2012-12-16
    • 1970-01-01
    • 2013-11-04
    • 2020-06-18
    相关资源
    最近更新 更多