首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >重定向到不同的操作而不使用查询字符串

重定向到不同的操作而不使用查询字符串
EN

Stack Overflow用户
提问于 2013-02-06 17:52:34
回答 1查看 116关注 0票数 1

我试图找出做以下事情的最佳方法(有几种方法我可以想到,但我想知道处理它的最佳方法是什么):

用户正在组装一批货物,然后单击“发送”链接,将其发送到/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)之类的操作?或者说这是最好的方法?

代码语言:javascript
运行
复制
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页面需要在发送地址是之后填写。

EN

回答 1

Stack Overflow用户

发布于 2013-02-06 18:09:28

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

代码语言:javascript
运行
复制
@shipment_id = @shipment.id
render new_shipping_address_path

在视野中

代码语言:javascript
运行
复制
<%= form_for @shipment_address do |f| %>
  <% if @shipment_id %>
    <%= hidden_field_tag :shipment_id, @shipment_id %>
  <% end %>

我不完全了解你的观点逻辑,但给出一个例子。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14735605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档