0

我目前在我的网站上_request_form创建新内容相当复杂。Requests目前,在创建请求时,员工必须从下拉菜单中选择他们的姓名,如下所示:

<%= f.collection_select :name, Employee.all(:order => 'name'), :name, :name %>

这个选择把右边Employee放在Request. 但是,如果员工不在数据库中,我想要一个other选项,collection_select它会生成两个文本框(用于 Employeenameemail),并且在提交表单时会生成新的Employee.

我认为这需要一些花哨的东西Ajax,但我有限的 Rails 知识并没有延伸那么远!

编辑:

这是我的完整观点:

<%= javascript_include_tag :defaults, "nested_form" %>
<div class="request_form">
<% if !@request.errors.empty? %>
  <div class="alert alert-error">
    <ul>
      <% @request.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>
<div class="well">
  <%= nested_form_for @request, html: { multipart: true } do |f| %>
    <%= f.label :title %><br />
    <%= f.text_field :title %><br /><br />
    <%= f.label :name, 'Submitted By' %><br />
    <%= f.select :name, Employee.sorted_employees_list.map { |value| [ value, value ] }, :id => "employee_box" %><br />
    <div id="new_employee_data">
    </div>
    <%= f.label :description %><br />
    <%= f.text_area :description %><br /><br />
    <%= f.label :attachment_uploader, 'Attachments' %><%= f.file_field :attachment_uploader, :multiple => true, name: "data_files[attachment_uploader][]" %><br />
    <% unless @request.data_files.empty? %>
      <%= f.label :attachment_uploader, 'Current Attachments:' %><br />
    <% end %>
    <%= f.fields_for :data_files do |attachment| %>
      <% if !attachment.object.new_record? %>
        <%= attachment.label :attachment_uploader, 'Delete: ' + attachment.object.attachment_uploader_url.split("/").last %>
        <%= attachment.check_box :_destroy %>
      <% end %>
    <% end %>
</div>
</div>
<script>
$(document).ready(function(){
  $('#employee_box').append("<option>Other</option>");
});

$('#employee_box').change(function() {
  if( $('#employee_box').val() === 'other' ) {
    $('#new_employee_data').append("<input type='text' id='employee_name' placeholder='Employee Name'> <br/> <br /></input><input type='email' id='employee_email' placeholder='Employee Email'>  </input>");
  }else {
    $('#employee_name').remove();
    $('#employee_email').remove();
  }
});
</script>

这包括@Kirti 的建议。但是,我似乎无法让它工作!

4

2 回答 2

3

我认为弹出对话框是不错的选择!

1)添加到您的 Gemfile,并运行捆绑:

gem 'jquery-ui-rails'

2)激活jquery-ui javascript(application.js):

//= require jquery.ui.dialog

3)链接jquery-ui样式表(application.css):

  *= require jquery.ui.dialog

4)为选择准备数据(employees_controller.rb)

  def new
    @prices = Price.all.map{|p| [p.price, p.id] }
    @prices << ['Create New', 'new_id']    
  end

5)在视图上显示选择组件(employees/new.html.erb):

  <%= select_tag :employee, options_for_select(@employees) %>

  <div id="new_employee_dialog">
    <label for="name"   type="text">Employee name:</label>
    <input name="name"  type="text"/>
    <label for="email"  type="text">Employee email:</label>
    <input name="email" type="email"/>
  </div>

6)这个javascript与对话窗口一起工作并发送ajax请求(assets/javascripts/employees.js.coffee):

$ ->
    $("#new_employee_dialog").dialog
      modal: true
      width: 400
      height: 300
      autoOpen: false
      draggable: false
      dialogClass: "without-header"
      buttons: [
        text: "Cancel"
        click: ->        
          $(this).dialog "close"
      ,
        text: "Ok"
        click: ->
          modalForm = $(this)
          $.post "/users/form_create",
            employee_name: $(modalForm).find("input[name='name']").val()
            employee_email: $(modalForm).find("input[name='email']").val()          
          , (data, status) ->          
            if data['status'] == 'ok'
              modalForm.dialog "close"
              alert "Ok"
            else
              alert "Oops"
      ]

    $('#employee').change ->
      selected_employee_id = jQuery("#employee").val()
      console.log('selected id: ' + selected_employee_id )
      if selected_employee_id == 'new_id'
        $("#new_employee_dialog").dialog("open");

7) 创建在服务器端 (employees_controller.rb) 捕获 ajax 请求的方法:

def form_create
    employee_name = params[:employee_name]
    employee_email = params[:employee_email]
    # create new user..
    respond_to do |format|
      format.json { render :json => {status: 'ok'} }
    end
  end

8)将其添加到routes.rb:

  post 'users/form_create' => 'users#form_create'

在此处输入图像描述

于 2014-03-08T22:20:50.987 回答
0

div在要生成两个输入字段的位置添加一个空标记(占位符):

<div id="new_employee_data">

</div>

jQuery在视图底部添加以下内容:

<script>
$(document).ready(function(){
  $('#request_name').append("<option value='other'>Other</option>");
});

$('#request_name').change(function() {
  if( $('#request_name').val() === 'other' ) {
    $('#new_employee_data').append("<input type='text' id='employee_name' placeholder='Employee Name'> <br/> <br /></input><input type='email' id='employee_email' placeholder='Employee Email'>  </input>");
  }else {
    $('#employee_name').remove();
    $('#employee_email').remove();
  }
});
</script>

在哪里,替换#request_nameid为您生成的collection_select.

您还需要添加用于action在表单提交时执行的创建新员工的代码。

注意:我不是 AJAX 专家,但您可以调整上面的 jQuery 并从那里获取它。

于 2014-03-08T21:50:33.347 回答