表单数据不存储在DB中

时间:2014-09-04 09:26:55

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

我的模特:

class LineItem < ActiveRecord::Base
  attr_accessible :itemable, :adults, :children
  belongs_to :itemable, polymorphic: true

  belongs_to :lead
  belongs_to :cart


end


class Lead < ActiveRecord::Base
    has_many :line_items, dependent: :destroy

  def add_line_items_from_cart(cart)
    cart.line_items.each do |item|
      item.cart_id = nil
      line_items << item
    end
  end

end


class House < ActiveRecord::Base
  has_many :line_items, :as => :itemable
end

class Appartment < ActiveRecord::Base
  has_many :line_items, :as => :itemable
end

item_controller

def create
      @line_item = @cart.line_items.build itemable: @object
      respond_to do |format|
        if @line_item.save
          format.html { redirect_to @line_item.cart,
            notice: 'Vakantiehuis toegevoegd in lijst.' }
          format.json { render action: 'show',
            status: :created, location: @line_item }
        else
          format.html { render action: 'new' }
          format.json { render json: @line_item.errors,
            status: :unprocessable_entity }
        end
      end
   end

private
    def create_object
      id = params[:house_id] || params[:appartment_id]

      model = "House" if params[:house_id]
      model = "Apartment" if params[:appartment_id]
      model = model.constantize

      @object = model.find(id)
   end

lead_controller

def create
    @lead = Lead.new(lead_params)
    @lead.add_line_items_from_cart(@cart)
    @lead.save

  end

cart_controller

def create
    @cart = Cart.new(cart_params)
end

private
def cart_params
      params[:cart]
    end

当从房屋和公寓页面添加项目时,访问者会转到购物车展示模板。访问者可以通过表格(选择)填写adutls和children的数量。

- @cart.line_items.each do |item|
    %tr.CartProduct


      %td
        .CartDescription
          %p
            = link_to item.itemable.name, polymorphic_url([item.itemable.country, item.itemable.region, item.itemable])
      %td.CartProductThumb
        %div
        - item.itemable.attachments.take(1).each do |a|
          = link_to(image_tag(a.file.url(:thumb)))   





      %td 
        .col-md-10
          = form_for(item) do |f|   
            = f.select :adults, ['1', '2', '3', '4', '5'], :class => 'form-control'

      %td 

        .col-md-10
          = form_for(item) do |f|   
            = f.select :children, ['1', '2', '3', '4', '5'], :class => 'form-control'

    = button_to "Checkout", new_lead_path, method: :get

[cart_id]和多态[itemable_type] [itemable_id]值正确存储在line_items表中,但不存储在表单数据中:成人和子项。我究竟做错了什么 ?谢谢remco

logfile:

Started POST "/nl/leads" for 127.0.0.1 at 2014-09-04 11:55:25 +0200
Processing by LeadsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"jmWa/Q/+SGsS+ed7zEB5+w5tiY2ywkEx3dQeVt9roKs=", "lead"=>{"firstname"=>"test ", "lastname"=>"", "email"=>"", "extra"=>"", "telephone"=>""}, "commit"=>"submit", "nl"=>"nl"}
  Cart Load (0.2ms)  SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = 25 LIMIT 1
  LineItem Load (0.2ms)  SELECT `line_items`.* FROM `line_items` WHERE `line_items`.`cart_id` = 25
   (0.1ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `leads` (`created_at`, `email`, `extra`, `firstname`, `lastname`, `telephone`, `updated_at`) VALUES ('2014-09-04 09:55:25', '', '', 'test ', '', '', '2014-09-04 09:55:25')
  SQL (0.2ms)  UPDATE `line_items` SET `lead_id` = 8, `updated_at` = '2014-09-04 09:55:25' WHERE `line_items`.`id` = 191
   (8.8ms)  COMMIT
Redirected to http://0.0.0.0:3000/nl/leads/8
Completed 302 Found in 40ms (ActiveRecord: 9.7ms)

0 个答案:

没有答案