'Rails view is not getting updated
I have a button click that triggers the following function. It calls the below asynchronous request.
<script>
function buy_now(user_id, deal_id) {
var purchase_confirmed = confirm("Are you sure you want to make this purchase?");
var theUrl = "/orders/create?user_id=" + user_id + "&deal_id=" + deal_id;
if(purchase_confirmed){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, true ); // true for asynchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
}
</script>
orders_controller.rb
class OrdersController < ApplicationController
def create
@order = Order.new(user_id: orders_params[:user_id], deal_id: orders_params[:deal_id])
unless @order.save
url = "/deals/today_deal?user_id=" + orders_params[:user_id]
redirect_to url, status: :unprocessable_entity
end
end
private
def orders_params
params.permit(:user_id, :deal_id)
end
end
create.html.erb
<h1> Order successful </h1>
routes.rb
ails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root 'deals#today_deal'
get '/deals', to: 'deals#index'
get '/deals/today', to: 'deals#today_deal'
get '/deals/new', to: 'deals#new'
get '/deals/:id', to: 'deals#show'
post '/deals/create', to: 'deals#create'
get '/orders/create', to: 'orders#create'
#set all incorrect paths to go to the root path
match '*path' => redirect('/'), via: :get
end
The problem is that once the create action(shown above) in the orders_controller is called and the subsequent view(shown above) is rendered the screen is not updated with the new view. I understand that I don't understand some concept here. It would great if you could point me to some resources to understand what I am doing wrong here. Thank you for your time.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
