I have a form_tag
in my Rails app. When I click on the submit button, the form is re-rendered. I would like to stop the re-rendering and I would like to retain the original form with all the input fields even when the submit button is clicked.
How can I achieve this? Pls help!
<%= form_tag generate_report_path(:header => true) do |f| % >
<div class="container-fluid">
<div style="padding-right:10px">
<%= select_tag(:report_id, options_for_select(
[["Select Report Type", 0],
["Report1", 1],
["Report2", 2],
["Report3", 3]]), id: "report_selection") %>
<%= hidden_field_tag :format, :pdf %>
I have a select_tag
in my form as shown above which the user uses to select the report which he would like to generate (report1 or 2 or 3)
Based on the selection above, a different set of input controls would be displayed right below the select_tag dropdown
shown above.
When the user selects the values he likes on the input controls and then clicks on the submit button (labelled as "generate report"), this is what happens:
select_tag dropdown
disappear leaving behind only the select_tag dropdown
with the default selection (so that the user can once again select the report which he would like to generate) How can I disable the above step and retain all the input controls (which were displayed below the select_tag dropdown
) even after the user clicks the submit button?
Pls help!
将HTML按钮替换为button_tag
> <%= button_tag "Generate Report", class: 'btn btn-sm btn-primary'%>
,这样可以解决页面刷新问题。
Form_tag support the remote option.
See
https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag
try
<%= form_tag(generate_report_path(:header => true), remote: true) do |f| % >
It changes the request into a xml request, so the browser won't refresh page after getting response.
You can read rails guides for more details.
https://guides.rubyonrails.org/working_with_javascript_in_rails.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.