RJS:Ajaxified select_tag [英] RJS: Ajaxified select_tag

查看:64
本文介绍了RJS:Ajaxified select_tag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我没有在我的上一个问题上得到预期的答案。我会尝试简化和缩小我的问题:

Since I didn't get the expected answer on my last question I'll try to simplify and narrow my question:


如何构建一个使用AJAX(无提交按钮)调用的下拉菜单某个控制器的显示动作?

How can I build a dropdown-menu that uses AJAX (no submit-button) to call the show action of a certain controller?

给出以下内容:

模型关联类别HABTM项目,因此下拉菜单包含所有类别名称。

Model-Association is Categories HABTM Projects, therefore the dropdown-menu consists of all category names.

查看部分,其中应显示下拉菜单。下拉菜单下方是一个项目列表,应根据下拉菜单中的选项进行更改:

The view partial where the dropdown-menu should be implemented. Below the dropdown menu is a list of projects that should change according to the choice made in the dropdown menu:

   <!-- placeholder for AJAX dropdown menu -->

   <!-- list of projects related to categories chosen by the select tag -->
   <ul class="projects">
     <% @projects.each do |_project| %>
       <li>
         <%= link_to(_project.name, _project) %>
       </li>
     <% end %>
   </ul>

带有应该调用的show-action的类别控制器: / p>

The Categories controller with the show-action that should be called:

class CategoriesController < ApplicationController
  def show
    # params[:id] should be the choice the user made in the dropdown menu
    @category = Category.find(params[:id])
    @projects = @category.projects.find(:all)

    respond_to do |format|
      format.html # show.html.erb
      format.js   # needed for ajax response?
    end
  end

  def index
    @projects = Category.find(params[:id]).projects.find(:all)
    @category = @project.categories.first

    respond_to do |format|
      format.html # index.html.erb
    end
  end 
end

路线用于调用类别控制器中的show-action:

The route to call the show-action in the Categories controller:

category GET    /categories/:id    {:controller=>"categories", :action=>"show"}

你会如何实现这个?任何帮助都非常苛刻!

How would you implement this? Any help is very apreciated!

推荐答案

这个怎么样:

<% form_for :category, :url => { :action => "show" } do |f| %>
  <%= select_tag :id, options_from_collection_for_select(Category.find(:all), :id, :name),
  { :onchange => "this.form.submit();"} %>
<% end %>

这将调用传统的html调用,因此它将刷新整个页面(并响应格式。 html)。

That will call a traditional html call, so it will refresh the entire page (and respond to format.html).

然后控制器将按提交的[:id]

Then the controller will find the category by the submitted [:id]

@category = Category.find(params[:id])

这篇关于RJS:Ajaxified select_tag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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