Ruby on Rails,表单数据不通过sql添加到数据库

时间:2017-04-18 13:01:12

标签: mysql sql ruby-on-rails ruby

在我的ruby网站上下订单时,只有一半的信息被添加到数据库中。 当我提交表单时,服务器返回以下内容:

Processing by OrdersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"bKcYiOq93T301ty4av2yyAqr4kKY11nn8I7qP7SWKmVgs/qgRmGcf82m5nAdnP1QqlvRFX8vjiSXa7dD4temwg==", "order"=>{"name"=>"Bruce Clay", "email"=>"bruce@dia.com", "address"=>"12", "city"=>"chippenham", "postcode"=>"kt12 3pw"}, "DeliveryDates.DeliveryConfirm"=>"false", "DeliveryDates.DeliveryDisagree"=>"True", "DeliveryDates.DeliveryDisagreeMessage"=>"", "ddate1"=>"20/04/2017", "ddate2"=>"25/04/2017", "ddate3"=>"23/04/2017", "card-identifier"=>"7FA7C55A-D3B3-45AC-AA82-D8850CD1E715"}

除了ddates选项之外,还会插入信息。

SQL (9.9ms)  INSERT INTO `orders` (`name`, `email`, `address`, `city`, `postcode`, `buyer_id`, `latitude`, `longitude`, `created_at`, `updated_at`) VALUES ('Bruce Claypot', 'bruce@dia.com', '12', 'chippenham', 'kt12 3pw', 1, 51.3862775, -0.3865515, '2017-04-18 12:50:21', '2017-04-18 12:50:21')

订单控制器有以下参数:

def order_params
  params.require(:order).permit(:name, :email, :address, :city, :county, :postcode, :ddate1, :ddate2, :ddate3)
end

并在def下创建:

@order = Order.new(order_params)

我真的迷失了造成这个问题的原因,因为我尝试更改了params选项,但没有任何方法可行。

感谢您的帮助。

ADDED订单模型。

class Order < ActiveRecord::Base
    belongs_to :user

    has_many :product_items, dependent: :destroy 
     has_many :statuses

    validates :name, :email, :address, :city, :postcode, presence: true


    geocoded_by :postcode

# def address
#   [address, postcode].compact.join(', ')
# end# can also be an IP address
after_validation :geocode# auto-fetch coordinates

    def add_product_items_from_basket(basket)
        basket.product_items.each do |item|
            item.basket_id = nil
            product_items << item
        end
    end

    def total_price
        product_items.map(&:total_price).sum
    end

STATUSES = ['pending', 'offered', 'accepted', 'declined', 'returned', 'completed', 'canceled', 'refunded']
validates_inclusion_of :status, :in => STATUSES,
          :message => "{{value}} must be in #{STATUSES.join ','}"

end

正在使用订单表单。

 <input class="text"  id="ddate1" name="ddate1" type="text" value=""><img class="ui-datepicker-trigger" src="../assets/calendar.png" alt="Select date" title="Select date">
                    </div>
                    <span class="field-validation-valid" data-valmsg-for="DeliveryDates.DeliveryDateFirst" data-valmsg-replace="true"></span>
                </th>
                <script>
    $('#order_ddate1').ready(function(){
           var date_input=$('input[name="ddate1"]'); //our date input has the name "date"
      var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : "body";
      var options={
        format: 'dd/mm/yyyy',
        startDate: "+2d",
        container: container,
        todayHighlight: true,
        autoclose: true,
      };
      date_input.datepicker(options);
    })
</script>

1 个答案:

答案 0 :(得分:1)

你已经设置了强大的参数:

$query_args = array(
                array (
                    'cat' => '6',
                    'orderby' => 'date',
                    'order' => 'DSC',
                    'posts_per_page' => '20'
                ),
                'orderby' => rand,
                'showposts' => '1'
            );  
$my_query = new WP_Query($query_args); 

表示您从订单对象中允许def order_params params.require(:order).permit(:name, :email, :address, :city, :county, :postcode, :ddate1, :ddate2, :ddate3) end ,即它们应该在订单哈希中 您的参数显示它们位于require对象ddate1, ddate2 and ddate3

之外
order

您需要像使用其他属性Parameters: {"utf8"=>"✓", "authenticity_token"=>"bKcYiOq93T301ty4av2yyAqr4kKY11nn8I7qP7SWKmVgs/qgRmGcf82m5nAdnP1QqlvRFX8vjiSXa7dD4temwg==", "order"=>{"name"=>"Bruce Clay", "email"=>"bruce@dia.com", "address"=>"12", "city"=>"chippenham", "postcode"=>"kt12 3pw"}, "DeliveryDates.DeliveryConfirm"=>"false", "DeliveryDates.DeliveryDisagree"=>"True", "DeliveryDates.DeliveryDisagreeMessage"=>"", "ddate1"=>"20/04/2017", "ddate2"=>"25/04/2017", "ddate3"=>"23/04/2017", "card-identifier"=>"7FA7C55A-D3B3-45AC-AA82-D8850CD1E715"} 一样将它们放在视野中 最好用它们作为:

name, email etc

其中f是表单对象迭代器。

我的猜测是您使用html语法为<%= f.text_field :ddate1, class="your_class" %> 生成表单元素。请改变它们

希望这有帮助