12

我正在尝试向我的 Spree 1.2 商店添加一个额外的步骤,这将允许客户创建订阅。我已经插入了步骤,并呈现了正确的视图,但是当用户单击“保存并继续”时,会呈现下一步,但实际上没有保存任何内容。

我知道我需要添加一个 state_callback,但我不确定如何执行此操作,并且 Spree 文档对此非常缺乏(大概是因为它很新)

目前我的扩展中有以下内容:

模型/狂欢/order_decorator.rb

Spree::Order.class_eval do
  belongs_to :subscription

  accepts_nested_attributes_for :subscription

  # This doesn't appear to be called
  Spree::Order.state_machine.after_transition :from => :subscription,
                                              :do => :valid_subs?

  checkout_flow do
    go_to_state :address
    go_to_state :subscription
    go_to_state :payment, :if => lambda { |order| order.payment_required? }
    go_to_state :confirm, :if => lambda { |order| order.confirmation_required? }
    go_to_state :complete
    remove_transition :from => :delivery, :to => :confirm
  end
end

不完全确定accepts_nested_attributes 是必要的,但我的开发方法到目前为止一直在反复试验,所以它最终留在那里。

在模型/订阅.rb

class Subscription < ActiveRecord::Base

  attr_accessible :start_date, :frequency

  belongs_to :user 
  has_many :orders
  has_many :products

  validates :start_date, :frequency, :presence => true

  def schedule
    ...code that returns a list of dates rendered on FE...
  end

  private #----

  ... some methods used in schedule ...

  def valid_subs?
    binding.pry # never called
  end

  def after_subscription
    binding.pry # never called either...
  end
end

意见/狂欢/结帐/_subscription.html.erb

<h1><%= t(:"subscription.title") %></h1>

<div class="columns alpha six" data-hook="subscription_calendar_fieldset_wrapper">
  <fieldset id="subscription_calendar" data-hook>
    <%= form.fields_for :subscription_picker do |subscription_picker| %>
      <legend><%= t(:"subscription.first_delivery") %></legend>
      <%= render :partial => 'subscription/picker' %>
    <% end %>
  </fieldset>
</div>

<div class="columns omega six" data-hook="subscription_dates_fieldset_wrapper">
  <fieldset id="subscription_dates" data-hook>
    <legend align="center"><%= t(:"subscription.next_deliveries") %></legend>
    <div class='dates'></div>
  </fieldset>
</div>

<div class="form-buttons" data-hook="buttons" style='clear:both;'>
  <%= submit_tag t(:save_and_continue), :class => 'continue button primary' %>
</div>

意见/订阅/_picker.html.erb

<div class='row'>
  <label for="subscription_frequency">Occurs every:</label>
  <% frequency_options = [["2 Weeks", 14], ["3 Weeks", 21], ["Month", 30], ["2 Months", 60], ["3 Months", 90]] %>
  <%= select(:subscription, :frequency, options_for_select(frequency_options, 30), {}) %>
</div>
<div id="start-date-picker" class="calendar"></div>
<%= hidden_field(:subscription, :start_date, {value: (DateTime.now + 14).to_date.iso8601}) %>

... JS that creates the calendar ...

单击“保存并继续”时,我看到发送了以下参数:

{
                  "utf8" => "✓",
               "_method" => "put",
    "authenticity_token" => "...BLAH...",
          "subscription" => {
         "frequency" => "30",
        "start_date" => "2012-11-17"
    },
                "commit" => "Save and Continue",
            "controller" => "spree/checkout",
                "action" => "update",
                 "state" => "subscription"
}
4

1 回答 1

4

你有两个问题。您的回调没有触发,因为对 checkout_flow 的调用会擦除当前状态机并替换它。在调用 checkout_flow 之后将此代码移至:

Spree::Order.state_machine.after_transition :from => :subscription,
                                          :do => :valid_subs?

其次,您的订​​阅信息没有保存,因为参数需要作为订单的一部分传递。参数应该像这样嵌套显示:

"order" => {    
    "subscription" => {
        "frequency" => "30",
        "start_date" => "2012-11-17"
    }
}

您可以通过将对 select 和 hidden_​​field 的调用附加到适当的表单“subscription_picker”来做到这一点:

<%= subscription_picker.select(:frequency, options_for_select(frequency_options, 30), {}) %>

<%= subscription_picker.hidden_field(:start_date, {value: (DateTime.now + 14).to_date.iso8601}) %>

您可能应该将表单对象作为样式和可理解性的显式参数传递给您的部分。

干杯。

于 2013-02-05T03:11:53.833 回答