Ruby on Rails,在创建Order之前为Order类添加表单字段?

时间:2015-05-20 02:32:13

标签: ruby-on-rails spree

我想问一些Ruby / Rails开发人员是否有可能。这是场景:

我有一个使用spree的电子商务应用。此应用程序可让您查看产品页面,并在指定数量时单击“添加到购物车”。此按钮触发创建订单的功能(如果之前不存在):

# orders_controller.rb
# Adds a new item to the order (creating a new order if none already exists)
def populate
  populator = Spree::OrderPopulator.new(current_order(create_order_if_necessary: true), current_currency)
  puts "current order = #{current_order}"
  if populator.populate(params[:variant_id], params[:quantity])
    respond_with(@order) do |format|
      format.html { redirect_to cart_path }
    end
  else
    flash[:error] = populator.errors.full_messages.join(" ")
    redirect_back_or_default(spree.root_path)
  end
end

我想自定义此内容,以便用户可以创建自定义订单。此自定义订单会包含其他字段:自定义消息和照片上传。我理想的工作流程是。

  1. 用户点击产品页面上的“自定义订单”。
  2. 模态打开。在那里,他们可以进入文本字段并上传图像。然后单击“添加到购物车”(仍在模态内)。
  3. 重定向到购物车页面,他们可以在此处结帐或继续购物。
  4. 我希望我可以实现这个工作流程,但我担心如果不严重破坏我的订单控制器就不可能。任何人都可以想到一种方法,我可以在实际创建订单类之前存储此自定义订单数据吗?这是NoSQL类型存储可以解决的问题吗?

    谢谢!

    编辑:我意识到我应该包含呈现“添加到购物车”按钮的html代码。似乎我正在使用的代码可能只是在它自己的工作。请注意,在文件顶部,它使用@orders模型启动一个表单(即使它尚未创建:

    #_cart_form.html.erb
    <%= form_for :order, :url => populate_orders_path do |f| %>
    <div id="inside-product-cart-form" data-hook="inside_product_cart_form" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
    <% if @product.price_in(current_currency) and !@product.price.nil? %>
      <div data-hook="product_price" class="columns five <% if !@product.has_variants? %> alpha <% else %> omega <% end %>">
    
        <div id="product-price">
    
          <div>
            <span class="price selling" itemprop="price">
              <%= display_price(@product) %>
            </span>
            <span itemprop="priceCurrency" content="<%= @product.currency %>"></span>
          </div>
    
          <% if @product.master.can_supply? %>
            <link itemprop="availability" href="http://schema.org/InStock" />
          <% elsif @product.variants.empty? %>
            <br />
            <span class="out-of-stock"><%= Spree.t(:out_of_stock) %></span>
          <% end %>
        </div>
    
          <% if @product.master.can_supply? %>
    
            <div class="add-to-cart">
              <%= number_field_tag :quantity, 1, :class => 'title', :min => 1 %>
              <%= button_tag :class => 'btn btn-success', :id => 'add-to-cart-button', :type => :submit do %>
                <%= Spree.t(:add_to_cart) %>
              <% end %>
            </div>
          <% elsif @product.variants.empty? %>
    
          <% end %>
      </div>
    <% else %>
        <div id="product-price">
          <br>
          <div><span class="price selling" itemprop="price"><%= Spree.t('product_not_available_in_this_currency') %></span></div>
        </div>
      <% end %>
      </div>
    <% end %>
    

    我已经在我的sql db中的orders表上设置了新字段,所以我将测试是否可以将其放入此处。

    另一个编辑我测试了这个,我在命令行中得到以下输出(在我的模态中点击添加到购物车后):

    Started POST "/store/orders/populate" for 127.0.0.1 at 2015-05-20 22:37:30 -0400
    Processing by Spree::OrdersController#populate as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"oD+iQ9av7+nynOisuGdcHDrxCBZxghtKq2uE/slpeyA=", "variant_id"=>"16", "order"=>{"custom_product_image"=>#<ActionDispatch::Http::UploadedFile:0x007fea642992c0 @tempfile=#<Tempfile:/var/folders/yj/hkslby052mxc7yn27ljx8fbh0000gp/T/RackMultipart20150520-2771-1f52ot9>, @original_filename="coffee1.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"order[custom_product_image]\"; filename=\"coffee1.jpeg\"\r\nContent-Type: image/jpeg\r\n">, "custom_product_description"=>"test"}, "quantity"=>"1", "button"=>""}
    

    我的新自定义字段为custom_product_descriptioncustom_product_image。它们都包含在此POST消息中,但是在生成的SQL语句中,它不会在SQL表中更新。有人能告诉我这里缺少的步骤是什么吗?这些字段在POST中作为params传递,但我需要做些什么才能将值插入到Postgres SQL中?

0 个答案:

没有答案
相关问题