集合\u在javascript的帮助下选择带参数的重定向

集合\u在javascript的帮助下选择带参数的重定向,javascript,ruby-on-rails,ruby,Javascript,Ruby On Rails,Ruby,当我更改下拉列表中的选项时,我正在尝试将用户重定向到“Attention/:domain”路由。我正在使用javascript来实现这一点。早期的实现,如:window.location=“'attention',:action=>'index')%>?“+id只需将id添加到前面的链接中,这样如果我有'attention/2',它就会变成'attention/23',而不是'attention/3'。因此,我尝试使用ruby提供的路径,如下所示: config.rb: get 'attenda

当我更改下拉列表中的选项时,我正在尝试将用户重定向到“Attention/:domain”路由。我正在使用javascript来实现这一点。早期的实现,如:
window.location=“'attention',:action=>'index')%>?“+id
只需将id添加到前面的链接中,这样如果我有
'attention/2'
,它就会变成
'attention/23'
,而不是
'attention/3'
。因此,我尝试使用ruby提供的路径,如下所示:

config.rb:

get 'attendance/:domain', to: 'attendance#index', as: 'attendance_domain'
视图:

<div class="form-group col-lg-3 col-md-5 ">
        <%= label_tag(:domain, "Domeniu", class: "control-label") %>
        <%= collection_select(nil, :id, @domains, :id, :complete_name, {selected: -1}, {:onchange=> "redirectToIndex(this.value)",:selected => params[:domain],class: "form-control"}) %>
    </div>

“redirectToIndex(this.value)”,:selected=>params[:domain],类:“表单控件”})%>
视图中的javascript:

function redirectToIndex(trainerId){
     window.location ="<%= url_for(attendance_domain_path(" + trainerId + ")) %>";
  } 
功能重定向到索引(trainerId){
window.location=“”;
} 
在这个实现中,我重定向到的链接是:
http://localhost:3000/attendance/ +trainerId+
这不是我想要的。它应该是该变量的值,而不是trainerId。有什么建议吗?

最后,我的问题是:如何使用collection\u select重定向参数。此下拉列表将在同一页面上可用,因此参数需要相应地更改,而不是像上面所述的情况。

您根本无法做到这一点-您不能调用ruby函数,在页面显示时使用javascript只知道的参数对服务器端进行评估(当然,除非您发出Ajax请求)

gem使用您的routes文件创建所有命名路由的javascript实现:您可以这样做

Routes.attendance_domain_path(id)

在javascript中。如果它只是一个路由,那么这可能有点过分。

只需将完整的url传递到
redirectToIndex()
collection\u选择
通过
proc
。在视图中尝试以下操作:

<%= collection_select(nil, :id, @domains, -> (d) {attendance_domain_path d},
  :complete_name, {selected: -1}, {:onchange=> "redirectToIndex(this.value)",
  :selected => params[:domain],class: "form-control"}) %>

}

->(d){url\u代表d},我用rails路径替换其中的d?
->(d){url\u代表d}
是一个
lambda
。Rails将为
@domains
中的每个项目调用它,select值将包含完整的url,而不仅仅是
id
。只要试一下代码,看看生成的HTML,就会发现语法错误、意外的tLPAREN_ARG、期望关键字_do_LAMBDA或tLAMBEG:D。给我几分钟时间来尝试解决这个问题你的Ruby和Rails版本是什么?gem'Rails'、'4.0.4'和Ruby类似于2.2.0这是5条路线我想:D但我想还是有点过头了是的,你的解决方案工作得很好,但Oleg也是,他先发了帖子,很抱歉我也无法检查你的路线
function redirectToIndex(val){
 window.location = val;