根据另一个更新选择框(Ruby on Rails) [英] updating a select box based on another (Ruby on Rails)

查看:81
本文介绍了根据另一个更新选择框(Ruby on Rails)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个更完整的示例,说明如何根据Ruby on Rails中第二个选择框的结果更新选择框.我已经在此处询问过.我已经阅读了该帖子的反馈信息,但是没有办法弄清楚这一点,而且我已经尝试了好几个小时.有人知道一个更好(更完整)的例子吗?

I'm in need of a more complete example on how to update a select box based on the results of a second select box in Ruby on Rails. I asked about this already here. I've read through the feedback from that posting, but am not having any luck figuring this out, and I've been trying for hours. Anybody know of a better (and more complete) example?

推荐答案

这通常用Javascript处理.我不是特别喜欢对Javascript进行编码,因此在我的应用程序中要做的是使用form_observer(使用Prototype Javascript库监视表单输入更改的Rails帮助器),并在包含HTML的HTML中更新DIV.第二个选择框,基于AJAX调用的结果.由于AJAX与服务器通信,因此我可以在Ruby中编写任意复杂的逻辑来呈现新的HTML.

This is generally handled in Javascript. I don't particularly enjoy coding Javascript, so what I do for this in my application is to use a form_observer (a Rails helper which uses the Prototype Javascript library to watch your form for input changes) and have update a DIV in the HTML containing the second select box, based on the results of an AJAX call. Since AJAX talks to my server, I can write arbitrarily complex logic in Ruby to render the new HTML.

示例代码:

#goes in view
<%= Code to render the first list box. %>
<%= render :partial => 'second_list_box_partial', :locals => {:selected = insert_magic_here } %>

<%= observe_field(:first_list_box,
    :url => { :action => :second_box_ajax }),
    :frequency => 0.5,
    :update => :second_list_box_div,
    :with => %Q| 'value=' + $('first_list_box').value;   |
    %>

#goes in controller
 def second_box_ajax
   first_box_value = params[:value]
   #magic goes here
   @selected = #more magic
   render :partial => 'second_list_box_partial', :locals => {:selected => @selected}, :layout => false
 end

 #goes in partial
 <div id="second_list_box_div">
   Actual code to render list box goes here.
 </div>

这篇关于根据另一个更新选择框(Ruby on Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆