简体   繁体   English

在运行时在Ruby on Rails中修改link_to

[英]Modifying link_to in Ruby on Rails at run time

I have a drop down menu which I populate with values from Database in the following way. 我有一个下拉菜单,通过以下方式用数据库中的值填充。

select(:user, i, User.all.where(:role => 'member').collect {|u| [u.email, u.id]})

i is a variable i keep incrementing as this is in a loop. 我是一个变量,我一直在递增,因为这是一个循环。 I iterate through a list and each row has this drop down box. 我遍历一个列表,每一行都有这个下拉框。 (I use the variable i, to generate a unique Id for each select box). (我使用变量i为每个选择框生成唯一的ID)。

For each row I have a link called checkout. 对于每一行,我都有一个称为checkout的链接。 On clicking checkout I want a pop up box (data-confirm), whether I want to proceed to checkout with the selected user from the drop down menu. 单击结帐后,我需要一个弹出框(数据确认),是否要继续从下拉菜单中与选定的用户结帐。 If the user presses yes, it has to go to the link I specify. 如果用户按“是”,则必须转到我指定的链接。 However I am not able to get the value of the selected dropdown menu. 但是,我无法获取所选下拉菜单的值。 I think it is due to mixing of javascript within ruby which probably isnt a good idea. 我认为这是由于在ruby中混合使用javascript可能不是一个好主意。 What would be the best way to proceed with this? 进行此操作的最佳方法是什么?

My checkout link is in the following way: 我的结帐链接采用以下方式:

<%= link_to "Checkout",book ,data: { confirm:{'Are you sure'}} %>

I know how to get the value of the selected drop down menu using Javascript, however I am not able to find a way to modify the values in the link_to field. 我知道如何使用Javascript获取所选下拉菜单的值,但是我无法找到一种方法来修改link_to字段中的值。 (I am using rails 4.2.4) (我正在使用Rails 4.2.4)

I'm having a tough time interpreting your question, but instead of a select field and a link, I think you might want to make a form like so: 我在解释您的问题时遇到了麻烦,但是我认为您可能想制作一个如下形式的表格,而不是选择字段和链接:

<%= form_tag book_path %>
  <%= select_tag "user", options_for_select(User.where(role: 'member').collect{ |u| [u.email, u.id] }), id: i %>
  <%= submit_tag 'Checkout', confirm: 'Are you sure?'
<% end %>

I'm not sure where your loop is at, so you'll have to adjust the code for that probably, but that's the gist of what you want I think. 我不确定您的循环在哪里,因此您可能必须为此调整代码,但这就是我想要的要点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM