简体   繁体   中英

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).

For each row I have a link called 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. 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. (I am using 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.

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.

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