Rails 4使用collection_select和jquery渲染部分表单

时间:2015-04-16 17:59:57

标签: jquery ajax ruby-on-rails-4 rendering ujs

我尝试创建一个根据collection_select

中选择的选项呈现不同部分的表单

以下是我开发的内容:

#new.html.erb     
<%= form_for(@person) do |f| %> 
        <div class="field"> 
            <%= f.label "Person type" %><br>
            <%= f.collection_select :person_type, Reg.where(daw_tabla_id: 20), :idregtab, :reg_char, {:include_blank => true}, {:multiple => false, :id => "tipop", :data => {:remote => true}}  %>
        </div>
<% end %>

collection_select给我选项0,1,2,3和4

基于选项选项,我想呈现_form.html.erb部分,然后使用jquery或ajax呈现_form1.html.erb_form2.html.erb ...

render_js.js.erb

$.jQuery(function(){
$('#tipop').on('change',function() {
  var val = document.getElementById('#tipop').value;
  <%= j render 'form'%>
  switch (val) {
    case "1":
      return <%= j render 'formestudent'%>;
    case "2":
      return console.log("val 2");
    case "3":
      return console.log("valor 3");
    case "4":
      return console.log("valor 4");
    default:
      return console.log("default");
  }
});

});

有人能给我一个线索吗?

0 个答案:

没有答案