Search code examples
ruby-on-railsformsrestroutesconfirmation

Order confirmation page in rails


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


Solution

  • 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.