简体   繁体   English

Ruby on Rails-在单击按钮时从Javascript下拉列表中选择的项目传递给Rails控制器

[英]Ruby on Rails - Pass item selected from a Javascript dropdown to Rails controller when a button is clicked

  • I have a button right next to a dropdown 我在下拉菜单旁边有一个按钮
  • 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.

相关问题 Ruby on Rails如何将数据从控制器传递到html中的javascript - Ruby on Rails how to pass data from controller to javascript in html 如何将 json 数据从 ruby controller 传递到 javascript(导轨)? - How to pass json data from ruby controller to javascript (Rails 6.1)? 单击按钮提交后,如何将下拉列表中的选定值传递给url查询字符串? - How can I pass the selected values from the dropdown list to the url query string when button submit is clicked? 从控制器Ruby on Rails 3.2.9调用JavaScript - Calling javascript from controller Ruby on Rails 3.2.9 从Rails上的javascript ruby​​调用控制器方法 - call a controller method from javascript ruby on rails Rails:如何将数据从Ruby传递到Javascript - Rails: how to pass data from Ruby to Javascript 如果单击按钮,将选择哪个下拉项和复选框? - Which dropdown item and checkbox are selected if button clicked? 在Rails中将数据从Controller传递到Javascript - Pass data from Controller to Javascript in Rails 如何在Ruby on Rails中将更新javascript变量传递给控制器​​? - How to pass updating javascript variable into form to controller in Ruby on Rails? 将JavaScript变量传递给Rails控制器上的ruby以进一步发送api - pass javascript variable to ruby on rails controller to send in api further
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM