Rails 中的订单确认页面

发布于 2024-08-14 11:40:03 字数 1698 浏览 15 评论 0原文

我一直在尝试为我的 Rails 应用程序创建一个订单确认页面,但不太确定如何以一种轻松的方式完成它。

关于这个问题有一些答案我已经完成了一半,但问题是我不太确定如何在 Rails 视图中设置表单,以便将用户带到包含所有详细信息的确认页面,而不是创建操作。

现在我的观点很简单:

        <% form_for :order do |f| %>
      <%= f.error_messages %>
      <p>
        <%= f.label :first_name %><br />
        <%= f.text_field :first_name, :size => 15 %>
      </p>
      <p>
        <%= f.label :last_name %><br />
        <%= f.text_field :last_name, :size => 15 %>
      </p>
      (Be sure to enter your name as it appears on your card)
      <p>
        <%= f.label :card_type %><br />
        <%= f.select :card_type, [["Visa", "visa"], ["MasterCard", "master"], ["Discover", "discover"], ["American Express", "american_express"]] %>
      </p>
      <p>
        <%= f.label :card_number %><br />
        <%= f.text_field :card_number %>
      </p>
      <p>
        <%= f.label :card_verification, "Card Verification Value (CVV)" %><br />
        <%= f.text_field :card_verification, :size => 3 %>
      </p>
      <p>
        <%= f.label :card_expires_on %><br />
        <%= f.date_select :card_expires_on, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :add_month_numbers => true %>
      </p>
  <p><%= f.submit "Submit" %></p>

我应该做什么才能将用户引导到显示所有订单详细信息的确认页面?

谢谢!

贤二

I've been trying to create an order confirmation page for my rails app, and am not quite sure how to go about it in a restful way.

There were a few answers on this question that got me halfway there, but the problem was that I wasn't quite sure how to set up the form in the rails view so that it would take the user to a confirmation page with all their details instead of a create action.

Right now my view is simple:

        <% form_for :order do |f| %>
      <%= f.error_messages %>
      <p>
        <%= f.label :first_name %><br />
        <%= f.text_field :first_name, :size => 15 %>
      </p>
      <p>
        <%= f.label :last_name %><br />
        <%= f.text_field :last_name, :size => 15 %>
      </p>
      (Be sure to enter your name as it appears on your card)
      <p>
        <%= f.label :card_type %><br />
        <%= f.select :card_type, [["Visa", "visa"], ["MasterCard", "master"], ["Discover", "discover"], ["American Express", "american_express"]] %>
      </p>
      <p>
        <%= f.label :card_number %><br />
        <%= f.text_field :card_number %>
      </p>
      <p>
        <%= f.label :card_verification, "Card Verification Value (CVV)" %><br />
        <%= f.text_field :card_verification, :size => 3 %>
      </p>
      <p>
        <%= f.label :card_expires_on %><br />
        <%= f.date_select :card_expires_on, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :add_month_numbers => true %>
      </p>
  <p><%= f.submit "Submit" %></p>

What things should I be doing to direct the user to a confirmation page that shows all the order details?

Thanks!

Kenji

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蘸点软妹酱 2024-08-21 11:40:03

对此有一些答案
让我半途而废的问题,
但问题是我不是
非常确定如何设置表格
Rails 视图,以便需要
用户进入确认页面
他们的所有详细信息而不是创建
行动。

将表单定向到非标准页面非常简单。

添加 URL 选项 form_for
这样

<% form_for :order do |f| %>

就变成了

<% form_for :order :url => {:action => "confirm"} do |f| %>

您需要在路线中创建确认操作,但这只涉及到:

map.resources :orders, :collection => {:confirm => :get}

您现在需要的是一个基本的控制器操作和一个视图:

def confirm
  @order = Order.new(params[:order])
  unless @order.valid?
    render :action => :new
  else       
  end
end

您的视图应该看起来与显示视图几乎相同,并添加了向 create 操作提交 @order 的表单。

There were a few answers on this
question that got me halfway there,
but the problem was that I wasn't
quite sure how to set up the form in
the rails view so that it would take
the user to a confirmation page with
all their details instead of a create
action.

Directing a form to a non standard page is pretty simple.

Add a url option form_for.
Such that

<% form_for :order do |f| %>

becomes

<% form_for :order :url => {:action => "confirm"} do |f| %>

You'll need to crate the confirm action in your routes, but that only involves this:

map.resources :orders, :collection => {:confirm => :get}

All you need now is a basic controller action and a view:

def confirm
  @order = Order.new(params[:order])
  unless @order.valid?
    render :action => :new
  else       
  end
end

Your view should look almost identical to the show view, with the addition of a form submitting @order to the create action.

说不完的你爱 2024-08-21 11:40:03

例如,为什么不通过 ajax 拉取确认,拉取结果并将其作为覆盖 div,在确认后提交表单中的原始值。

如果您仍然需要按照自己的方式进行操作,请检查wizardly,它正是为此类用途而设计的。

Why don't you pull the confirmation via ajax for example, pull the result and put it as an overlay div, upon confirmation submit the original values in the form.

If you still need to do it your way then check wizardly, it's exactly designed for such uses.

孤檠 2024-08-21 11:40:03

我想更新更优雅的 Rails 4 或更高版本的答案。
我希望它能帮助像我这样的新手。鲁比太棒了! :)

routes.rb

resources :orders do
  collection do
    post 'confirm'
  end
 end

order_controller.rb

def confirm
  @order = Order.new(order_params) # GET THE POST parameters
  render :new if @order.invalid? # Return if false
end

form.html.erb

<%= form_for @order, url: {action: 'confirm'} do |f| %>

I would like to update the answer for more elegant Rails 4 or up.
I hope it will help newbies like me. Ruby is awesome! :)

routes.rb

resources :orders do
  collection do
    post 'confirm'
  end
 end

orders_controller.rb

def confirm
  @order = Order.new(order_params) # GET THE POST parameters
  render :new if @order.invalid? # Return if false
end

form.html.erb

<%= form_for @order, url: {action: 'confirm'} do |f| %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文