我试图找出做以下事情的最佳方法(有几种方法我可以想到,但我想知道处理它的最佳方法是什么):
用户正在组装一批货物,然后单击“发送”链接,将其发送到/shipments/:id/confirm页面。confirm操作检查用户是否有已完成的ShippingAddress;如果没有,则将其发送到ShippingAddress#new。(如果他这样做了,它会呈现confirm页面。
我希望用户能够完成ShippingAddress#new页面,提交它,然后重定向回/shipments/:id/confirm。我怎么能这么做?如何才能将:id传递到ShippingAddress#new页面,而无需在Shipment#confirm操作中执行redirect_to new_shipping_address_path(shipment_id: @shipment.id)之类的操作?或者说这是最好的方法?
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页面需要在发送地址是之后填写。
发布于 2013-02-06 18:09:28
尝试调用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 %>我不完全了解你的观点逻辑,但给出一个例子。
https://stackoverflow.com/questions/14735605
复制相似问题