Reputation: 988
<% select_tag(:report_id, options_for_select(
[["Detail1", 1], ["Detail2", 2], ["Detail3", 3], ["Detail4", 4], ["Detail5", 5], ["Detail6", 6], ["Detail7", 7]]))%>
<% submit_tag("Generate Report") %>
I have the above select menu. This would return report_id parameter based on the value selected by the user. Now, I need another parameter named format to be set as :xlsx if report_id is 1 or 2 and format should be set to :pdf if report_id > 2. How can I achieve this? Please help!
I tried something like this but this does not work:
<% select_tag(:report_id, options_for_select(
[["Detail1", 1], ["Detail2", 2], ["Detail3", 3], ["Detail4", 4], ["Detail5", 5], ["Detail6", 6], ["Detail7", 7]]))%>
<% if report_id < 3 %>
<%= hidden_field_tag :format, :xlsx %>
<% elsif report_id >=3 %>
<%= hidden_field_tag :format, :pdf %>
<% end %>
<% submit_tag("Generate Report") %>
I get this error with the above code:
Please help!
Upvotes: 0
Views: 1071
Reputation: 944
You can't access value of report_id
untill unless you submit it to server. You can use JS/jquery to update format
parameter value whenever the select_tag value changes
<% select_tag(:report_id, options_for_select(
[["Detail1", 1], ["Detail2", 2], ["Detail3", 3], ["Detail4", 4], ["Detail5", 5], ["Detail6", 6], ["Detail7", 7]]), id: "report_id")%>
<%= hidden_field_tag :format, :xlsx, id: "format_param" %>
<% submit_tag("Generate Report") %>
JS to assign format params value whenever select_tag value changes
$("#report_id").change(function(){
var report_val = $("#report_id").val();
if(report_val < 3)
$('#format_param').val("xlsx")
else
$('#format_param').val("pdf")
})
Upvotes: 1
Reputation: 521
Here you can do two things.
You can write if condition in your controller once the action is trigger
if params[:report_id] >= 3
format = 'pdf'
else
format = 'xlsx'
end
OR if you want send from form itself then you need to write jquery onchange event
<% select_tag(:report_id, options_for_select(
[["Detail1", 1], ["Detail2", 2], ["Detail3", 3], ["Detail4", 4], ["Detail5", 5], ["Detail6", 6], ["Detail7", 7]])) id: "something"%>
Jquery
$('#something').on('change', function() {
if(this.value >= 3) {
$('#hidden_value').val('pdf')
} else {
$('#hidden_value').val('xlsx');
}
})
hidden field
<% hidden_field_tag :format , id: "hidden_value" %>
Upvotes: 1