解析单选按钮输入的值并保存到数据库时难以调试 rails 应用程序



我正在尝试从表单(如"890-UPS Standard"(中获取字符串值并将其解析为数据库中的两个字段(order.shipping获取数字价格,order.shipping_choice获取运输方式的字符串(。

我的应用程序中charges#shipping视图中有以下表单情况:

    <% shipping_choices = [] %>
    <% @ups_rates.each do |rate| %>
      <% choice = [] %>
      <% choice << rate[1].to_s + "-" + rate[0].to_s %> <!-- value -->
      <% choice << number_to_currency(rate[1]/100).to_s + " - " + rate[0].to_s %>
      <% shipping_choices << choice %>
    <% end %>
  <%= simple_form_for @order, url: charges_update_order_path(:shipping), method: :post do |f| %>
    <div class="row">
      <div class="form-inputs text-left">
        <div class="form-group col-sm-6">
          <%= f.collection_radio_buttons :shipping, shipping_choices, :first, :last, item_wrapper_class: :block_radio_button_collection %>
        </div>
      </div> <!-- form inputs -->
    </div> <!-- choices row -->
    <div class="row">
    <%= f.button :submit, "Calculate Shipping" %>
  </div>
  <% end %>

我的order.rb模型中有一个方法,它应该值并将其转换为适当的格式:

  def update_order_from_shipping_page(shipping_pair)
    self.shipping = shipping_pair.gsub(/[^d]/, '').to_f
    self.shipping_choice = shipping_pair.gsub(/A[a-zs]+Z/, '')
    new_total = self.total + shipping
    self.update_attributes(total: new_total, shipping_choice: self.shipping_choice)
  end

这是表单生成的 HTML,以便清楚地了解发生了什么:

<div class="form-group col-sm-6">
    <span class="block_radio_button_collection"><label for="order_shipping_1820-ups_ground"><input type="radio" value="1820-UPS Ground" name="order[shipping]" id="order_shipping_1820-ups_ground" /><label class="collection_radio_buttons" for="order_shipping_1820-ups_ground">$18.00 - UPS Ground</label></label></span>
    <span class="block_radio_button_collection"><label for="order_shipping_4292-ups_three-day_select"><input type="radio" value="4292-UPS Three-Day Select" name="order[shipping]" id="order_shipping_4292-ups_three-day_select" /><label class="collection_radio_buttons" for="order_shipping_4292-ups_three-day_select">$42.00 - UPS Three-Day Select</label></label></span>
    <span class="block_radio_button_collection"><label for="order_shipping_6673-ups_second_day_air"><input type="radio" value="6673-UPS Second Day Air" name="order[shipping]" id="order_shipping_6673-ups_second_day_air" /><label class="collection_radio_buttons" for="order_shipping_6673-ups_second_day_air">$66.00 - UPS Second Day Air</label></label></span>
    <span class="block_radio_button_collection"><label for="order_shipping_13851-ups_next_day_air_saver"><input type="radio" value="13851-UPS Next Day Air Saver" name="order[shipping]" id="order_shipping_13851-ups_next_day_air_saver" /><label class="collection_radio_buttons" for="order_shipping_13851-ups_next_day_air_saver">$138.00 - UPS Next Day Air Saver</label></label></span>
</div>

我目前在charges_controller中收到update_order方法的no implicit conversion of Symbol into Integer错误:

  def update_order
    @order = current_order
    if @order.update_order_from_shipping_page(params[:order][:shipping][:shipping_choice])
      redirect_to new_charge_path and return
    else
      redirect_to :back
      flash[:notice] = "Something is amuck."
    end
  end

谁能看出我哪里出了问题?

事实证明我使用了太多参数,因此当我将(params[:order][:shipping][:shipping_choice])更改为(params[:order][:shipping])时,它开始工作。

最新更新