简体   繁体   English

将选定的下拉值作为参数传递给rails link_to

[英]pass selected drop down value as params in rails link_to

I have a select tag which populates list of department names, When I select a value from drop down, I need to display an edit link below (link_to) and append the selected value of dropdown as id params.(I am not using form for this single select tag since I dont need to save value in my databse). 我有一个选择标签,用于填充部门名称列表,当我从下拉列表中选择一个值时,我需要在(link_to)下面显示一个编辑链接,并将选择的下拉列表值附加为id参数。(我没有使用form这个单一的选择标记,因为我不需要在数据库中保存值)。 How this could be possible. 这怎么可能。 Please help. 请帮忙。 I am using rails2.3 我正在使用rails2.3

I tried like the below but it didn't work. 我尝试如下所示,但没有成功。

  Select A Dept
    <%=select :select_dept, :dept, @dept.map{|dept| [dept.full_name, dept.id]},{:prompt => "#{t('select_a_batch')}"} %>
     <%= link_to "Display", {:controller => "user", :action => "display_value", :id => $('#select_dept').val() } ,:class => "button" %>

Gopal is on the right track. Gopal处在正确的轨道上。

Give the link an id so you can pick it out 给链接一个ID,以便您可以选择它

<%= link_to 'Display", {:controller ...}, :class => 'button', :id => 'display_link' %>

Hide it with CSS by default 默认情况下用CSS隐藏它

#display_link { display:none; }
#display_link.showing { display:block; }

Then in javascript, do something like 然后在javascript中做类似的事情

$('#select_dept').on('change', function(ev) {
   var deptId = $(this).find(':selected').val();
   var newPath = '/users/' + deptId + '/display_value';
   $('#display_link').attr('href', newPath);
   $('#display_link').addClass('showing');
});

This will not set things up on page load, but you could run the same function you've passed as the change callback on page load to set things up (assuming one of the items might be selected on load). 这不会在页面加载时进行设置,但是您可以运行与页面加载时的change回调传递的功能相同的函数来进行设置(假设在加载时可能选择了其中一项)。

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

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