RoR无法将数据从输入数据库中获取

时间:2015-02-07 18:44:37

标签: ruby-on-rails ruby ruby-on-rails-3

我试图添加一个新专栏" company"到现有的模型" order.rb"一切似乎都运行良好,但我似乎无法从输入字段中获取数据到数据库中的新列。

我错过了什么?

数据库中的测试数据:

[1] pry(main)> Order.all
  Order Load (0.1ms)  SELECT "orders".* FROM "orders"
=> [#<Order token: nil, charge_transaction: nil, address_one: "777 Knoll Ln.", address_two: "P.O. Box 743", city: "Los Angeles", state: "CA", zip: "90015", country: "US", status: "SC", number: nil, user_id: "1", shipping: #<BigDecimal:69eed90,'0.595E3',9(36)>, tracking_number: nil, phone: "(626) 513-5035", expiration: "2015-04-03", created_at: "2015-02-06 20:07:04", updated_at: "2015-02-06 20:07:04", payment_option_id: nil, campaign_id: 1, full_name: "John Connor", quantity: 1, terms_of_service: true, order_uri: nil, tax: #<BigDecimal:69f3070,'0.0',9(36)>, company: nil>]

我可以看到公司专栏(最后),但无论我在表格上输入什么,我总是得到零值。

分贝/迁移/ 20150206193142_add_company_to_orders.rb

class AddCompanyToOrders < ActiveRecord::Migration
  def change
    add_column :orders, :company, :string
  end
end

应用程序/控制器/ orders_controller.rb     def create

    @order = Order.new(
      :user => current_user,
      :campaign => @campaign,
      :quantity => params[:order][:quantity],
      :company => params[:order][:company],
      :full_name => params[:order][:full_name],
      :address_one => params[:order][:address_one],
      :address_two => params[:order][:address_two],
      :city => params[:order][:city],
      :state => params[:order][:state],
      :zip => params[:order][:zip],
      :phone => params[:order][:phone],
      :country => params[:order][:country],
      :terms_of_service => params[:order][:terms_of_service]
    )

应用程序/视图/命令/ _form.html.erb

%= field_set_tag "Shipping Address", :id => "address" do %>

        <%= f.input :company, :label => "Company" %>
        <%= f.input :full_name, :label => "Full Name" %>
        <%= f.input :address_one, :label => "Street Address 1" %>
        <%= f.input :address_two, :label => "Street Address 2" %>
        <%= f.input :city %>
        <%= f.input :zip, :label => "ZIP or Postal Code", :input_html => { :class => "zip" } %>

<% end %>

1 个答案:

答案 0 :(得分:0)

您在单独的迁移中添加了:公司,但似乎您忘记在模型中添加attr_accessible


如果您的应用正在运行rails 3,请将其添加到Order.rb文件中:

attr_accessible :company

<小时/> 对于rails 4,将该属性添加到order_controller.rb私有方法

def order_params
      params.require(:message).permit(:company, :et_al)
end


<小时/> 的 参考文献:
attr_accessor - http://apidock.com/rails/ActiveModel/MassAssignmentSecurity/ClassMethods/attr_accessible