0

我有以下看法:

index.html.js
<h5>Select your state:</h5>
<%= select_tag "state", options_for_select(@states.collect{|x| x[0]}), :onchange => '$.get("/home/populate_options")' %>

其中@states 是一组状态缩写,例如:

@states
  [
    ['AK', 'AK'],
    ['AL', 'AL'],
    . . .
  ]

如何通过 ajax 将包含所选状态值的参数(如果选择阿拉斯加,应将“AK”发送到控制器操作)传递给我的控制器操作?

我是否必须在发送之前在我的 javascript 中手动构建 url $.get("/home/populate_options"),使其看起来更像$.get("/home/populate_options/AK")?也许我需要改变我使用助手的方式,这样我才能真正使用'rails-like'助手并使用符号发送参数:state =>“AK”?

4

1 回答 1

1

尝试这个:

当你有@states = [['AK','AK'],['AL','AL']]这样的。你不需要使用collect@states足够了。

对于在参数中发送值,onchange您可以执行以下操作:

  <%= select_tag "state", options_for_select(@states), :onchange => '$.get("/home/populate_options?state="+$(this).val())' %>

但在您的问题中,不清楚您究竟从哪里填充数据ajax

于 2013-03-27T14:14:24.240 回答