繁体   English   中英

Ruby on Rails select2 AJAX / JQuery-将参数传递给控制器​​不起作用

[英]Ruby on Rails select2 AJAX/JQuery - passing a parameter to controller not working

我正在尝试制作“职位描述(JD)”页面,以在选择的JD之间进行比较。 我的主页上将有一个用于选择某些作业的字段,以及一个按钮/链接,用于使用带有RoR的select2在select字段下方的表中显示所选作业的详细信息,如图像Job descriptions Viewer所示

我的问题是我无法将所选的Jobs ID传递给控制器​​,并且收到以下消息:

已完成406在20毫秒内不可接受(ActiveRecord:0.0毫秒)ActionController :: UnknownFormat(ActionController :: UnknownFormat):app / controllers / job_descriptions_controller.rb:81:在updateJobs中

我的控制器方法:

def updateJobs
  @selected = JobDescription.where(id: params[:selectJdField2])
  respond_to do |format|
   format.js
   format.html 
  end
end

主视图(jdComparison.html.erb)将呈现两个局部视图

<h1>Listing Job Descriptions</h1>
<%= render 'sidebar' %>
   <div class="clearfix"></div>
<div>
  <%= render partial: 'item_list', locals: { job_desc: @job_descriptions} %>
</div>

_sidebar.html.erb部分具有selet2字段和一个刷新Jds的链接,该链接称为“查找链接”:

<div class="col-sm-8">
list of JDs:
   <%= select_tag "selectJdField", options_from_collection_for_select(@job_descriptions, :id, :job_title), { :multiple => true } %>
</div>
<%= link_to "Find Link", updateJobs_path(@job_descriptions),  :remote => true %>
<script>
  $(document).ready(function() { $("#selectJdField").select2(); });
</script>

_item_list.html.erb部分将查看在select2字段中已选择的所有JD:

<div>
 <table>
  <thead>
    <tr>
     <th>Job title</th>
     <th>Department</th>
    </tr>
  </thead>
  <tbody>
  <% job_desc.each do |job_description| %>
    <tr>
      <td><%= job_description.job_title %></td>
      <td><%= job_description.department %></td>
    </tr>
   <% end %>
  </tbody>
 </table>
</div>

updateJobs.js.erb,当我单击“查找链接”按钮时应刷新JD列表(我认为这是我的第一个问题)

$("#div_id").html("<%= escape_javascript(render partial: 'item_list', locals: { job_desc: @selected}) %>")

JS文件(我想我的第二个问题在这里):

$ ->
 updateLists = () ->
  $.ajax  
  url:'updateJobs'
  type: 'post'
  dataType: 'html'
  format: 'js'
  data:   {
    selectJdField2 :   $('#selectJdField').val()   
  }

路线:

get 'updateJobs'        =>  'job_descriptions#updateJobs'
post 'updateJobs'        =>  'job_descriptions#updateJobs'

当我用这个替换控制器方法时:

def updateJobs
  @selected = JobDescription.where(id: 1)
end

单击查找链接后,它将为我提供JD编号1的详细信息。 请您帮忙。

为了使它起作用,我做了以下操作:我已经使用普通按钮重现了“查找链接”:

<button id="btnFind">Find</button>

我在_item_list.html.erb部分的第一个div中添加了“ id”

<div id = "div_id">

在js文件中:

$ ->
  $(document).on 'change', '#selectJdField', (evt) ->
      updateLists()
  $(document).on 'click', "#btnFind", (evt) ->
     updateLists()
  updateLists = () ->
   $.ajax  '/updateJobs',
    type: 'GET'
    dataType: 'script'
    data:   {
     selectJdField2 :   $('#selectJdField').val()   
    }

而且我不需要在路由文件中(发布“ updateJobs”)。现在,一切正常……谢谢“ Venkat Ch”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM