使用ruby on rails将boolean值保存到postgres数据库

时间:2017-08-02 10:43:55

标签: ruby-on-rails ruby postgresql radio-button e-commerce

我现在正在做一个旧的rails项目。由于我在一段时间内没有使用导轨,所以我有点生疏,需要一些帮助。

问题是我在电子商务网站上添加了一个提货选项。

后端由active admin

驱动

当客户检查其产品时。 他可以选择在商店取货或送货。

我希望订单在后端显示是否应该发送产品或是否在商店领取产品。

我有点卡在这里,交付选项似乎没有保存到数据库......如果有人可以帮助我,这将是非常好的。

这是拾取选项的html。它位于views/orders/_form.html.erb

<div class="col-md-5 pick-up-buttons" id="country_div">
  <li>
    <%= f.radio_button :pick_up, "1", checked: false, data: { question: "Pick up your items in the store" } %> 
    <%= f.label :pick_up, "Pick up your items in the store" %>
  </li>
  <li>
    <%= f.radio_button :pick_up, "0", checked: true,  data: { question: "Send the items by mail" } %> 
    <%= f.label :pick_up, "Send the items by mail" %>
  </li>
</div> 

这是schema.rb文件的一部分

create_table "orders", force: :cascade do |t|
  t.string   "name"
  t.string   "email"
  t.text     "address"
  t.string   "city"
  t.string   "country"
  t.datetime "created_at",                 null: false
  t.datetime "updated_at",                 null: false
  t.boolean  "shipped",    default: false
  t.boolean  "pick_up",    default: false
  t.string   "delivery"
end

这是orders_controller.rb

class OrdersController < ApplicationController
  include CurrentCart
  before_action :set_cart, only: [:new, :create]
  before_action :set_order, only: [:show, :edit, :destroy]

  def index
    @orders = Order.all? 
  end

  def new
    @images  = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"]
    @random_no = rand(5)
    @random_image = @images[@random_no]

    if @cart.product_items.empty?
      redirect_to root_url, notice: 'Your Cart is Empty'
      return
    end

    @order = Order.new
    @client_token = Braintree::ClientToken.generate

    @del_cost_euro = 20
  end

  def create
    @order = Order.new(order_params)
    if @order.save
      charge
      if @result.success?
        @order.add_product_items_from_cart(@cart)
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        OrderNotifier.received(@order).deliver 
        redirect_to root_url, notice: 'Thank You for Your Order'
      else
        flash[:error] = 'Please Check Your Cart'
        redirect_to root_url, alert: @result.message
        @order.destroy
      end
    else
      @client_token = Braintree::ClientToken.generate
      render :new
    end
  end

  def show
  end

  def destroy
    @order.destroy
    redirect_to root_url, notice: 'Order deleted'
  end

  private
  def set_order
    @order = Order.find(params[:id])
  end

  def order_params
    params.require(:order).permit(:name, :email, :address, :city, :country, :pick_up, :delivery)
  end

  def charge
    @result = Braintree::Transaction.sale(
      amount: @cart.total_price_usd,
      payment_method_nonce: params[:payment_method_nonce] )
  end
end

这是app/admin/order.rb

ActiveAdmin.register Order do

permit_params :shipped

after_update do |order|
  OrderNotifier.shipped(@order).deliver if order.shipped
end

show do |order|
  panel 'Customer Details' do
    attributes_table_for order, :name, :email, :address, :city, :country
  end

  panel 'Created' do
    "#{time_ago_in_words order.created_at} ago"
  end

  panel 'Shipped' do
    order.shipped
  end

  panel 'delivery' do
    order.pick_up
  end

  panel 'Order Details' do
    table_for(order.product_items) do 
      column 'Product' do |item|
        item.product.title
      end

      column 'Quantity' do |item|
        item.quantity
      end

      column 'Price Euro' do |item|
        number_to_currency item.total_price_eur
      end

      column 'Price USD' do |item|
        number_to_currency item.total_price_usd
      end
    end
  end

  panel 'Order Total USD' do 
    number_to_currency order.total_price_usd
  end

  panel 'Order Total Euro' do 
    number_to_currency order.total_price_eur
  end
end

1 个答案:

答案 0 :(得分:2)

您想知道该商品是在商店领取还是通过电子邮件发送正确?

由于你迷路了,一个更简单的解决方案是:

1 - 将t.boolean“pick_up”更改为t.string“pick_up”

2 - 使用此http://guides.rubyonrails.org/form_helpers.html#the-select-and-option-tags,创建一个包含这两个选项的列表。

3 - 在您的控制器中保存用户想要的选项。

我认为为了使用单选按钮,您的数据库中必须有两个字段。像这样:

t.boolean“pick_up” t.boolean“sent_email”

如果用户选择接听,您将收到一个具有真值的参数,然后您可以保存在您的数据库上。所以这也是另一种选择!

希望它有所帮助。