如何在Rails中进行动态下拉? [英] How to make dynamic dropdown in Rails?

查看:87
本文介绍了如何在Rails中进行动态下拉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个电子商务项目,其中将有用于选择尺寸的单选按钮。我有一个数量下拉列表。我想根据用户所选尺寸的库存使这个下拉动态。任何人都可以告诉我如何在Rails上做到这一点?没有用大量的javascript混乱我的视图文件!?

I'm working on a e-commerce project where there will be radio buttons for size selection. I have a dropdown for quantity. I want to make this dropdown dynamic based on stock available for the user selected size. Can anyone tell me how this can be done on Rails? without cluttering my view file with lot of javascript!?

推荐答案

如果你使用Rails 3和原型(默认afaik),你可以使用原型遗留助手( Github上的链接)添加观察者并为您的下拉框渲染部分视图。您可以添加这样的观察者

If you are using Rails 3 with prototype (default afaik), you could use the prototype legacy helpers (Link on Github) to add an observer and render a partial view for your dropdown box. You could add an observer like this

<%= observe_field 'element_var_name',
      :url => { :action => "another_action_here" },
      :update => "div_tag_to_update",
      :with => "'selected='+ escape($('element_var_name').value)" %>

请务必调整 element_var_name 并执行操作根据你的情况。 div_tag_to_update 是将使用下拉框更新的div。 another_action_here 操作应该呈现如下视图:

Be sure to adjust element_var_name and the action to your situation. div_tag_to_update is the div that will update with the dropdown box. The another_action_here action should render a view like this:

def call_ids_by_type
  @element_list = ... # whatever fits for you, like: [[key, value],[key, value]]
  render :layout => false
end

在局部视图中,您可以使用元素列表生成下拉列表框:

In the partial view, you can use the element list to generate the dropdown box:

<%= f.select :var_name, @element_list %>

这篇关于如何在Rails中进行动态下拉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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