Reputation: 666
My controller action perform_review
is responding with json. As long as there are an order to fetch, a view partial is re-rendered by perform_review.js.erb
with the new order, but when we are out of orders to review, perform_review.js.erb
should redirect to an other page.
The controller looks like this:
## orders_controller.rb
class OrdersController < ApplicationController
def perform_review
...
@order = get_order
respond_to do |format|
format.html {
redirect_to orders_path(current_user)
}
format.js {
if @order == nil
@redirect_path = orders_path
return
end
}
end
end
end
And here is the js.erb file:
## perform_review.js.erb
if ("<%=@redirect_path %>" != "") {
location.href = "<%=@redirect_path %>";
}
else
var order_options_partial = $("#order_options_partial");
order_options_partial.children().remove();
order_options_partial.append('<%= j render("order_options") %>');
The else
part works just fine, but the if part does not. The curious thing is that everything is OK in the controller, and @redirect_path
gets assigned with "/orders". However, it looks like perform_review.js.erb
does not get triggered after this. To be sure of this I have tried to put in a line that said console.log("Starting perform_review.js.erb");
, but it does not get called in the browser console...
Upvotes: 1
Views: 1308
Reputation: 5734
You can do it in controller as well
class OrdersController < ApplicationController
def perform_review
...
@order = get_order
respond_to do |format|
format.html {
redirect_to orders_path(current_user)
}
format.js {render :js => "window.location.href = '"+orders_path(current_user)+"'"} if @order.blank?
end
end
end
In this way you do not need the if else in your perform_review.js.erb
.
And if you want to do in the view file, then here is the following code.
<% if (@order.blank?) %>
window.location.href = "<%= escape_javascript(@redirect_path) %>";
<% else %>
// your code
<% end %>
Upvotes: 2