Timothé Gauguet
Timothé Gauguet

Reputation: 183

How to reload the page after action : create?

When I attempt to add an item to the cart, it works but nobody can see it directly because the page is not reloading. How can I fix it ?

Here's my order_item_controller :

class OrderItemsController < ApplicationController

  def create
    @order = current_order
    @order_item = @order.order_items.new(order_item_params)
    @order.save
    session[:order_id] = @order.id
  end

and my views/product, with form_for :

    <%= form_for order_item, remote: true do |f| %>
      <%= product.firstdescription %>
      <p class="bonus1">-Durée du cours : <%= product.duration %> minutes</p>
      <p class="bonus">-<%= product.info %>
      <p class="bonus">-En préparation</br>
      <div class="talent">
      <%= f.hidden_field :quantity, value: 1, class: "form-control", min: 1, max: 1 %>
      <%= f.hidden_field :product_id, value: product.id %>
      <div class="on_precommand">
        <p id="old_price">
          <del><%= currency_euro product.old_price %></del>
        </p>
        <p><%= currency_euro product.price %></p></br>
        <%= product.tournage %></br>
      </div>
      <%= f.submit "Pré-commander", class: "addtocart" %>
    <% end %>

Upvotes: 1

Views: 2151

Answers (1)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42799

All you need to do is a simple redirect, add this line at the end of the action

redirect_to @order

This will redirect to the show action of the new order.

EDIT:

Just noticed you are doing a remote: true request, so instead you need to create a js template create.js.erb for example, and add the javascript you want to be executed to add the created order in the view, here's an example:

$('#orders').append('<%= j( render @order ) %>')

Of coruse this is assuming that the orders div is has an id #orders

Upvotes: 2

Related Questions