【发布时间】:2013-05-20 07:55:32
【问题描述】:
我正在尝试从 select_tag 调用辅助方法。我试过这个:
<%= select_tag(:themes, options_for_select({"Black" => "compiled/styles", "Green" => "compiled/styles2"})) %>
当我输入 alert(this.options[this.selectedIndex].value) 而不是调用方法时,它可以工作。如何更改我的代码以根据需要调用该方法?
编辑
我的application.js上有这个
$(document).ready(function() {
$('#display_themes').change(function(){
$.ajax({url: '<%= url_for :controller => 'application', :action => 'set_themes()', :id => 'this.value' %>',
data: 'selected=' + this.value,
dataType: 'script'
})
})
});
在控制器中我有 set_themes 方法
def set_themes()
@themes = params[:id]
respond_to do |format|
redirect_to '/galleries'
format.html # index.html.erb
format.xml { render :xml => @themes }
end
end
现在的问题是@themes 仍然为空,即使我更改了下拉菜单
Routes.rb
match '', :controller => 'application', :action => 'set_themes()'
【问题讨论】:
-
显然 remote_function 在 Rails 3 中不起作用
-
你能说明
set_themes是如何初始化的吗?它做了什么? -
请查看编辑后的版本。谢谢:)
标签: ruby-on-rails-3 onchange helper html-select