1

我正在尝试找出执行以下操作的最佳方法(我能想到几种方法,但我想知道处理它的最佳方法是什么):


用户正在整理货物,然后单击“发送”链接,该链接将他发送到该/shipments/:id/confirm页面。该confirm操作检查用户是否完成了ShippingAddress; 如果没有,它会将他发送到ShippingAddress#new. (如果他这样做了,它会渲染confirm页面。

我希望用户能够完成ShippingAddress#new页面,提交它,然后被重定向回/shipments/:id/confirm. 我怎样才能做到这一点?如何在不执行类似操作的情况下将其传递:id到页面?或者这是最好的方法吗?ShippingAddress#newredirect_to new_shipping_address_path(shipment_id: @shipment.id)Shipment#confirm


class ShipmentsController < ApplicationController
  def confirm
    @shipment = Shipment.where(id: params[:id]).first

    unless current_user.has_a_shipping_address?
        # Trying to avoid having a query string, but right now would do the below:
        #   in reality, there's a bit more logic in my controller, handling the cases
        #   where i should redirect to the CardProfiles instead, or where I don't pass the
        #   shipment_id, and instead use the default shipment.
        redirect_to new_shipping_address_path(shipment_id: @shipment.id)
    end
  end
end


class ShippingAddressesController < ApplicationController
  def new
    @shipment = Shipment.where(id: params[:shipment_id]).first
  end

  def create
    @shipment = Shipment.where(id: params[:shipment_id]).first

    redirect_to confirm_shipment_path(@shipment)
  end
end

【其实在收货地址之后还有一个CardProfiles#new页面需要填写】。

4

1 回答 1

1

尝试调用 render 而不是 redirect_to,并将 id 设置为实例变量。调整视图逻辑以提取该实例变量(如果存在)。

@shipment_id = @shipment.id
render new_shipping_address_path

在视图中

<%= form_for @shipment_address do |f| %>
  <% if @shipment_id %>
    <%= hidden_field_tag :shipment_id, @shipment_id %>
  <% end %>

我不完全了解您的视图逻辑,但举个例子。

于 2013-02-06T18:09:28.420 回答