的Javascript
function update_versions_div(project_id) {
jQuery.ajax({
url: "/update_versions",
type: "GET",
data: {"project_id" : project_id},
dataType: "html"
success: function(data) {
jQuery("#versionsDiv").html(data);
}
});
}
控制器
def edit
@projects = Project.all
@versions = Version.all
end
def update_versions
@versions = Version.where(project_id => params[:project_id]).all
render :partial => "versions", :object => @versions
end
查看
<%= select_tag "project_id", options_from_collection_for_select(@projects, "id", "title"), :prompt => "Select a project", :onchange => "update_versions_div(this.value)" %>
<div id="versionsDiv">
<%= render :partial => 'versions', :object => @versions %>
</div>
部分:_version.html.erb
<%= select_tag "version_id", options_from_collection_for_select(versions, "id", "title"), :prompt => "Select a version" %>
另外在你的routes.rb中添加对/update_versions
路线
match "/update_versions" => "<controller>#update_versions"
在这里,你应该与控制器的名称替换<controller>
。
我还没有测试代码,所以可能有错误。
更新
PullMonkey已更新使用Rails 3为例,这是比这个代码明显优于代码。请结帐http://pullmonkey.com/2012/08/11/dynamic-select-boxes-ruby-on-rails-3/也
看到这个[这个问题](http://stackoverflow.com/questions/5472045/multilevel-select-on-rails) – rubyprince
@rubyprince我已经写了关于轨道3(!!)。在Rails 3 –
oh ..sorry中不存在remote_function,在这种情况下,您可以用jQuery中的某些东西替换onchange函数...我会用它发布一个答案... – rubyprince