[英]Ruby on Rails - Pass item selected from a Javascript dropdown to Rails controller when a button is clicked
When a user selects a value from the dropdown and clicks on the button, the value from the dropdown should be passed as a parameter to the Rails controller function 当用户从下拉菜单中选择一个值并单击按钮时,下拉菜单中的值应作为参数传递给Rails控制器功能
This is the code that I'm using: 这是我正在使用的代码:
<%= link_to 'Generate Report', forecast_report_pdf_path(format: :pdf), onclick: "GetSelectedItem('select1')", class: 'btn btn-primary' %>
how do I pass the value returned by the GetSelectedItem function to my controller? 如何将GetSelectedItem函数返回的值传递给控制器? (I already pass format = pdf as a parameter now; but I am not sure how to pass the value returned by GetSelectedItem as well)
(我现在已经将format = pdf作为参数传递;但是我不确定如何传递GetSelectedItem返回的值)
I don't if this is the best solution but you could use a form inside the dropdown so you select the option and the value is sent to the controller. 我不是最好的解决方案,但是您可以在下拉菜单中使用表格,以便选择该选项并将其值发送到控制器。
Here you have an example but feel free to adapt it to your code: 在这里,您有一个示例,但可以根据您的代码进行调整:
<%= form_for :pdf, url: forecast_report_pdf_path(format: :pdf) |form| %>
<%= select_tag(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %>
<%= form.text_field :name %>
<%= form.submit 'Generate Report', class: 'btn btn-primary'%>
<% end %>
Check rails documentation for more details: https://guides.rubyonrails.org/form_helpers.html 查看rails文档以获取更多详细信息: https : //guides.rubyonrails.org/form_helpers.html
<%= form_tag forecast_report_pdf_path do %>
<%= select_tag(:report_id, options_for_select([["Summary1", 7], ["Summary2", 6]])) %>
<%= hidden_field_tag :format, :pdf %>
<%= submit_tag("Generate Report") %>
I finally got around to writing the above code and it worked!! 我终于写了上面的代码,它成功了!! Thanks much for your inputs @nicolasnisoria !
非常感谢您的输入@nicolasnisoria!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.