RoR:form.select + onchange没有将正确的值传递给控制器 [英] RoR : form.select + onchange not passing the right value in params to the controller
问题描述
我在Ruby on Rails窗体中有一个选择菜单,定义如下:
<%= f.select: Menu1,[[Option1,value1],[Option2,value2]],{},{:id =>Menu1_Id,class = Menu1_Class}%>
我使用事件处理程序来触发控制器操作,当选择一个选项, it the selected of the selected option
< script type =text / JavaScriptsrc = http://code.jquery .com / jquery-latest.js>
$(function(){
$('。Menu1_Class')。bind('change',function(){
alert (this).val());
$ .ajax('#{:controller =>TestsController,:action =>show}?param_one ='+ $(this).val ));
});
});
< / script>
编辑:这是对我工作的js代码感谢罗宾的回答下面(注意我使用它的行动get_results而不是原来的一个显示):
< script type =text / JavaScript>
$(function(){
$('menu_class') .bind('change',function(){
$ .ajax({
url:<%= get_results_my_tests_url%>,
data:{
param_one: $(this).val()
}
});
});
});
< / script>
#GET / tests / 1
#GET /tests/1.json
def show
@test = Test.find(params [:id])
如果params [:param_one] .present?
@ blah = params [:param_one]
输入show:@blah =+ @ blah.to_s +\\\
end
respond_to do | format |
format.html#show.html.erb
format.json {render:json => @test}
end
end
正确的值(value1或value2),但是param_one不存在,因此puts不会为param_one返回任何内容,而我期望看到value1或value2。
任何人都可以请我指出我在哪里做错了?
感谢
解决方案>将您的javascript更改为以下内容:
< script type =text / JavaScriptsrc = http: /code.jquery.com/jquery-latest.js\">
$(function(){
$('。Menu1_Class')。bind('change',function(){
alert($(this).val());
$ .ajax({
url:<%= test_path(@test)%>,
data:{ param_one:$(this).val()}
});
});
});
< / script>
I have a select menu in a Ruby on Rails form defined as follows:
<%= f.select :Menu1, [["Option1","value1"],["Option2","value2"]], {}, {:id=>"Menu1_Id", :class => "Menu1_Class"} %>
I am using an event handler to trigger controller action when an option is selected and I want to pass to it the value of the selected option
<script type="text/JavaScript" src=http://code.jquery.com/jquery-latest.js"> $(function(){ $('.Menu1_Class').bind('change', function(){ alert($(this).val()); $.ajax('#{:controller => "TestsController", :action => "show"}?param_one='+$(this).val()); }); }); </script>
EDIT : this is the js code that worked for me thanks to Robin's answer below (notice I'm using it for action "get_results" instead of the original one "show"):
<script type="text/JavaScript"> $(function(){ $('.menu_class').bind('change', function(){ $.ajax({ url: "<%= get_results_my_tests_url %>", data: { param_one: $(this).val() } }); }); }); </script>
in my controller
# GET /tests/1 # GET /tests/1.json def show @test = Test.find(params[:id]) if params[:param_one].present? @blah=params[:param_one] puts "show : @blah = " + @blah.to_s + "\n" end respond_to do |format| format.html # show.html.erb format.json { render :json => @test } end end
With this, the alert message contains the right value (value1 or value2) but param_one is not present so puts returns nothing for "param_one", while I expect to see "value1" or "value2" there.
Anyone can please point me to where am I doing something wrong?
Thanks
解决方案Change your javascript to the following:
<script type="text/JavaScript" src=http://code.jquery.com/jquery-latest.js"> $(function(){ $('.Menu1_Class').bind('change', function(){ alert($(this).val()); $.ajax({ url: "<%= test_path(@test) %>", data: { param_one: $(this).val() } }); }); }); </script>
这篇关于RoR:form.select + onchange没有将正确的值传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文