使用 Ajax 将项目动态添加到 Ruby On Rails 中的选择列表

发布于 2024-09-13 23:33:12 字数 614 浏览 2 评论 0原文

我已经为此查看了一大堆解决方案,也许我只是很笨,但我无法让任何东西发挥作用。这些是我试图在产品/新视图中结合在一起的两个选择,这将允许用户选择产品类别,然后限制子类别列表:

 <p>
   <%= f.label :category_id %>: 
   <%=  f.select("category", Category.find(:all).collect {|c|[c.name, c.id]})%>
 </p>
 <p>
   <%= f.label :subcategory_id %>:
   <%=  f.select("subcategory", Subcategory.find(:all).collect {|s|[s.name, s.id]})%>
 </p>

我的子类别控制器中有一个函数可以从数据库获取信息:

  def get_subcategories
    @subcategories = Subcategory.find_by_category_id(:all)
  end

但我不知道如何将它连接在一起,而且看起来在 RoR 中应该很容易。

I have looked at a whole bunch of solutions for this and maybe I am just dense, but I can't get anything to work. These are the two selects I am trying to tie together in a product/new view that will allow the user to select a category of product and then constrain the subcategory listing:

 <p>
   <%= f.label :category_id %>: 
   <%=  f.select("category", Category.find(:all).collect {|c|[c.name, c.id]})%>
 </p>
 <p>
   <%= f.label :subcategory_id %>:
   <%=  f.select("subcategory", Subcategory.find(:all).collect {|s|[s.name, s.id]})%>
 </p>

I have a function in my subcategory controller to get the information from the database:

  def get_subcategories
    @subcategories = Subcategory.find_by_category_id(:all)
  end

But I can't figure out how to wire it together, and it seems like it should be easy in RoR.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2024-09-20 23:33:12

嗯,这并不完全是微不足道的,但只要付出一点努力就可以实现。基本方法是:

  • 最初呈现视图时填充类别选择列表
  • 添加要在更改时触发的 javascript 处理程序
  • 在此处理程序中对服务器进行 AJAX 调用并调用您的 get_subcategories 方法在控制器操作内
  • 让控制器操作响应填充子类别选择列表的值
  • 让 AJAX 响应处理程序更新子类别选择列表的 HTML
  • 如果您希望使用默认值填充类别选择列表,则当表单最初呈现时,您将希望在那时自动进行 AJAX 调用,甚至在用户选择任何内容之前。

例如,在 JQuery 中,您可以将其与以下方法结合起来

  • .change() 在选择列表上设置处理程序
  • .triggerEvent('change') 将导致加载后触发的事件(如果需要)
  • $.getJSON() 可用于获取和解析 AJAX 响应
  • .remove().append () 可用于清除子类别选择并用结果更新

Well it's not completely trivial but it is achievable with a little effort. The basic approach would be:

  • Populate the category select list when the view is initially rendered
  • Add a javascript handler to be triggered when this is changed
  • Do an AJAX call to the server within this handler and call your get_subcategories method within a controller action
  • Have the controller action respond with the values for populating the subcategory select list
  • Have the AJAX response handler update the HTML for the subcategory select list
  • In the case where you want the category select list to be populated with a default value when the form is initially rendered you will want to have the AJAX call automatically made at that time, even before the user has selected anything.

For example, in JQuery you could put this together with the following methods

  • .change() sets up the handler on the select list
  • .triggerEvent('change') would cause the event to fire after load (if you need that)
  • $.getJSON() can be used to fetch and parse the AJAX response
  • .remove() and .append() can be used to clear and update the subcategory select with the result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文