【问题标题】:Checkboxes, Boolean Values, and Spree复选框、布尔值和狂欢
【发布时间】:2014-10-09 13:44:34
【问题描述】:

我创建了一个表单,用于询问用户的信息,并显示他们购物车中的商品及其价格和小计。提交此表单后,他们会转到包含信息的展示页面,底部是一个“创建报价单”按钮,该按钮会为他们的假设购买生成 PDF 报价单。

这是我的问题:我想在表单上为代表非营利组织的用户设置一个复选框,以便可以应用折扣。我希望这个折扣在信息提交后出现在“显示”页面上(稍后在生成的 pdf 上出现,但一次出现一个问题)。为此,我在报价数据库中为布尔值“非营利”创建了一个列。总的来说,我对编程很陌生,所以我一直在阅读我可以在 rails 和 checkboxes 上找到的所有内容,但这些信息要么不适用,要么过时,要么太难理解。

作为记录,我知道目前的代码存在严重的安全问题(现在任何人都可以查找任何报价),我打算稍后解决。我也尝试在这里缩短我的代码,因为使用了很多文件。

控制器:

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 只是说“render _form”,所以这里是 _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 %>

_调整:

<%=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 %>

我认为我只是从根本上误解了复选框的工作原理,但由于我一直未能找到可以提供帮助的东西,所以我想请教大家。

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

【问题讨论】:

    标签: ruby-on-rails forms checkbox spree


    【解决方案1】:

    在您的表单中添加

       <%= f.check_box :nonprofit %> 
    

    然后是显示页面中的 if 语句

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-04
      • 2011-10-14
      • 2013-12-09
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      相关资源
      最近更新 更多