Ruby on Rails select2 AJAX/JQuery - 将参数传递给控制器不工作
Ruby on Rails select2 AJAX/JQuery - passing a parameter to controller not working
我正在尝试制作职位描述 (JD) 页面以比较所选的 JD。
我的主页将有一个 select 一些职位的字段,以及 button/link 在 selection 字段下方的 table 中显示 selected 职位详细信息的字段使用 select2 和 RoR,如图所示
Job descriptions Viewer.
我的问题是我无法将 selected 作业 ID 传递给控制器,我收到此消息:
Completed 406 Not Acceptable in 20ms (ActiveRecord: 0.0ms)
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/job_descriptions_controller.rb:81:in `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字段和一个link来刷新调用"Find Link"的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,当我点击"Find Link"按钮时应该刷新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
点击查找Link后会给我京东1号的详细信息。拜托,我需要你的帮助..
这是我为使它起作用所做的工作:
我用一个普通的按钮使 "Find Link" 复发了:
<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()
}
而且我在路由文件中不需要 (post 'updateJobs').. 现在,一切正常...谢谢 "Venkat Ch"
我正在尝试制作职位描述 (JD) 页面以比较所选的 JD。 我的主页将有一个 select 一些职位的字段,以及 button/link 在 selection 字段下方的 table 中显示 selected 职位详细信息的字段使用 select2 和 RoR,如图所示 Job descriptions Viewer.
我的问题是我无法将 selected 作业 ID 传递给控制器,我收到此消息:
Completed 406 Not Acceptable in 20ms (ActiveRecord: 0.0ms) ActionController::UnknownFormat (ActionController::UnknownFormat): app/controllers/job_descriptions_controller.rb:81:in `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字段和一个link来刷新调用"Find Link"的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,当我点击"Find Link"按钮时应该刷新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
点击查找Link后会给我京东1号的详细信息。拜托,我需要你的帮助..
这是我为使它起作用所做的工作: 我用一个普通的按钮使 "Find Link" 复发了:
<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()
}
而且我在路由文件中不需要 (post 'updateJobs').. 现在,一切正常...谢谢 "Venkat Ch"