Currently, I'm in the process of developing an application that requires a remote form feature. As I work on this, I've realized that I have never fully grasped how Rails' respond_to
function operates.
Let's consider the code snippet below within the view:
<%= form_tag ..., :remote => true %>
<%= submit_tag "submit" %>
<% end %>
Assuming we have a javascript partial named _home.js.erb
, with an action related to it named home
.
If I have the following code in my controller:
def home
# ...
respond_to do |format|
format.html
format.js { render :partial => 'home' }
end
end
With this setup, clicking the submit button on the form will trigger the execution of the JavaScript in _home.js.erb
. However, what's happening behind the scenes when the submit button is clicked? How does Rails determine whether to respond_to
HTML or JavaScript?
Your insights are much appreciated. Thank you!