Reputation: 56799
I have a situation where:
.js.erb
file.Despite trying a number of approaches, Rails 5 renders the .js.erb literally, showing me the actual code in the view.
How can one get a Rails redirect_to
to happen in JS format so the js.erb file is run properly?
In Rails 4.2, this works:
redirect_to custom_data_path(...), format: 'js'
In Rails 5.2, I've tried:
# 1
redirect_to custom_data_path(...), format: 'js'
# 2
redirect_to custom_data_path(...), format: :js
# 3
respond_to do |format|
format.js { redirect_to custom_data_path(...) }
end
# 4
request.format = :js
respond_to do |format|
format.js { redirect_to custom_data_path(...) }
end
Upvotes: 0
Views: 1164
Reputation: 1045
I had the same problem where my format (json) was not preserved while redirecting
I solved it with (Rails 5):
redirect_to _path(, params: request.query_parameters.merge(format: params[:format]))
Upvotes: 1
Reputation: 15848
I don't think there's a concept or "redirect" for js requests. You can always render a script that does a request to the "redirected" url you want.
Instead of
format.js { redirect_to custom_data_path(...) }
You could have a view for that action with
//I'm using query just as an example, but it can be done with vanilla javascript o any js framework
$.getScript('<%= custom_data_path -%>');
Upvotes: 0