复选框,布尔值和Spree

时间:2014-08-15 19:27:35

标签: ruby-on-rails forms checkbox spree

我已经创建了一个表单,用于询问用户的信息,并显示其购物车中的内容及其价格和小计。一旦他们提交了这个表格,他们会进入一个包含相关信息的节目页面,在底部是一个"创建报价"按钮,为他们的假设购买生成PDF报价。

这是我的问题:我希望在表单上为代表公益组织的用户设置一个复选框,以便可以应用折扣。我希望这个折扣出现在&#34; show&#34;提交信息后的页面(以及稍后生成的pdf,但一次只有一个问题)。我在引号数据库中创建了一个列,其布尔值为&#34;非营利组织&#34;为此目的。我一般都很喜欢编程,所以我一直在阅读我在轨道和复选框上找到的所有内容,但这些信息要么不适用,要么过时,要么难以理解。< / p>

为了记录,我意识到现在的代码存在严重的安全问题(任何人都可以立即查找任何引用),我打算稍后解决。我也试图在这里缩短我的代码,因为使用了很多文件。

控制器:

module Spree
class QuotesController < Spree::BaseController
before_action :set_quote, only: [:edit, :destroy, :show]

# GET /quotes
# GET /quotes.json
helper Spree::StoreHelper
helper Spree::BaseHelper
include Spree::Core::ControllerHelpers::Order

def current_currency
  (defines currency)
end

# GET /quotes/1
# GET /quotes/1.json
def show
end

# GET /quotes/new
def new
  @quote = Quote.new
  if current_order
    @quote.set_order(current_order)
  else
    redirect_to :back, notice: Spree.t(:your_order_is_empty_add_product)
  end
end

# GET /quotes/1/edit
def edit

end

# POST /quotes
# POST /quotes.json
def create
  @quote = Quote.new(quote_params)
  @quote.set_order(current_order)

  respond_to do |format|
    if @quote.save
      format.html { redirect_to @quote, notice: 'Quote was successfully created.' }
      format.json { render action: 'show', status: :created, location: @quote }
    else
      format.html { render action: 'new' }
      format.json { render json: @quote.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /quotes/1
# PATCH/PUT /quotes/1.json
def update
  set_quote
  (updates quote)
    end
  end
end

# DELETE /quotes/1
# DELETE /quotes/1.json
def destroy
  (destroys quote)
  end
end

def pdf
  set_quote
  template = params[:template] || "quote"
  render :layout => false, :template => "spree/quotes/#{template}.pdf.prawn"
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_quote
    @quote = Quote.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def quote_params
    params.require(:quote).permit(:first_name, :last_name, :company, :address1, :address2, :city, :country, :state, :zipcode, :phone, :email, :other)
  end
  end
  end

型号:

module Spree
class Quote < ActiveRecord::Base
    validates :first_name, :last_name, :address1, :city, :country, presence: true
    has_many :quote_line_items


    def set_order(order)
  total = 0
        order.line_items.each do |item|
            l = QuoteLineItem.new
            l.sku = item.variant.product.sku 
            l.description = item.variant.product.name + " " + item.variant.options_text
            l.quantity = item.quantity
            l.price = item.variant.product.price
    l.peritemsubtotal = item.variant.product.price * item.quantity
    total = total + l.peritemsubtotal
            l.quote = self
            #l.save!
            quote_line_items << l
        end

        self.subtotal = total

    end 
(this is just one of several attempts I've made).
    def adjustments
        if self.nonprofit == true
                total = total * 0.85
            end
        self.total = total
    end
    end
    end

New.html.erb只是说&#34;渲染_form&#34;,所以这里_form

<%= form_for(@quote) do |f| %>
<% if @quote.errors.any? %>
(throws error message)
<% end %>

<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name, :class => 'required' %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name, :class => 'required' %>

(etc. for other pertinent fields)

<%=Spree.t(:which_product)%>
<%= render :partial => '/spree/shared/product_quote' %>
<%= render :partial => '/spree/shared/adjustments'%>

<%= button_tag("Create Quote") %>
<% end %>

_adjustments:

<%=Spree.t(:special_circumstances)%>
<br>
<%= check_box_tag(:nonprofit)%>
<%= label_tag(:nonprofit, "This product is for a nonprofit")%>
<br>

...和节目页面:

<p id="notice"><%= notice %></p>

<p>
<strong>First name:</strong>
<%= @quote.first_name %>
</p>

<p>
<strong>Last name:</strong>
<%= @quote.last_name %>
</p>

(etc. for other fields)

<!--There needs to be some code in here to display what adjustments were selected-->

<p>Final Total: <%= @quote.total %> </p>

<%= button_to "Print Quote", "/quotes/#{@quote.id}/pdf", :method => "get" %>
<br>
<p> <%= Spree.t(:is_correct) %> 
<%= link_to 'Edit Information.', edit_quote_path(@quote) %> </p>
<br>
<%= link_to 'Back', quotes_path %>

我认为我从根本上误解了复选框的工作方式,但由于我找不到帮助的方法,我一直都在问你。

编辑:是否有人选择成为非营利组织也需要在我们的数据库中注册,而不仅仅是在显示页面的显示中。

1 个答案:

答案 0 :(得分:1)

在您的表单中添加

   <%= f.check_box :nonprofit %> 

然后是节目页面中的if语句