Ruby on rails 尝试显示控制器索引操作时,Rails 4 AJAX请求不起作用
我正在构建一个小应用程序,有一个后端供用户添加/编辑/删除办公室位置,在面向公众的网站上,我有一个模式,我想在其中显示办公室位置,在后端,所有内容都加载到索引视图中,没有问题,但是当我尝试将位置加载到面向公众的模式中时,没有显示任何内容 当我尝试在inspect(检查)窗口chrome中对此进行故障排除时,当我单击链接以启动模式时,什么也没有发生 这里的任何帮助都将不胜感激,因为AJAX对我来说还有些陌生 我的AJAX请求位于app/views/layout/application.html.erb中 文件:Ruby on rails 尝试显示控制器索引操作时,Rails 4 AJAX请求不起作用,ruby-on-rails,ajax,ujs,Ruby On Rails,Ajax,Ujs,我正在构建一个小应用程序,有一个后端供用户添加/编辑/删除办公室位置,在面向公众的网站上,我有一个模式,我想在其中显示办公室位置,在后端,所有内容都加载到索引视图中,没有问题,但是当我尝试将位置加载到面向公众的模式中时,没有显示任何内容 当我尝试在inspect(检查)窗口chrome中对此进行故障排除时,当我单击链接以启动模式时,什么也没有发生 这里的任何帮助都将不胜感激,因为AJAX对我来说还有些陌生 我的AJAX请求位于app/views/layout/application.html.e
我认为您需要完成AJAX请求。您正在向/locations路径发出GET请求,我假设该路径呈现部分?您需要捕获AJAX请求的响应,并告诉浏览器将其放置在页面的何处 您可以在ERB文件中将其作为脚本标记,但在assets文件夹的JavaScript树中,它可能会工作得更好 使用jQuery时,典型的AJAX请求如下所示:
$("#publicLocation").on("click", function() {
$.ajax({
url: '/locations',
method: 'GET'
}).done(function(response){
// Tell the app where to put the response here
// the response contains your partial, which you can append to an empty
// div in your html.
}).fail(function(error){
console.log(error)
})
})
谢谢,我刚刚用类似的方法解决了这个问题!我将获得最佳奖,因为这和我所做的差不多!
class LocationsController < ApplicationController
before_action :set_location, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /locations
# GET /locations.json
def index
@locations = Location.all
@location = Location.new
end
end
<div class="modal fade" id="publicLocation" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<table class="table" id="container_locations">
<thead>
<tr>
<th><center>city</center></th>
<th><center>tel number</center></th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% if @locations.present? %>
<div id="containerLocations">
<%= render @locations %>
</div>
<% else %>
<td colspan="11"><center><h5>no locations added. please add your first location now.</h5></center></td>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<tbody>
<tr id="location_<%= location.id %>">
<td class="hidden-sm-down"><center><%= location.unit_number %></center></td>
<td class="hidden-sm-down"><center><%= location.street_number %></center></td>
<td class="hidden-sm-down"><center><%= location.street_name %></center></td>
<td class="hidden-sm-down"><center><%= location.quad %></center></td>
<td><center><%= location.city %></center></td>
<td class="hidden-sm-down"><center><%= location.province %></center></td>
<td class="hidden-sm-down"><center><%= location.postal_code %></center></td>
<td><center><%= location.tel_number %></center></td>
<td><center><%= link_to 'Show', location %></center></td>
<td><center><%= link_to 'Edit', edit_location_path(location) %></center></td>
<td><center><%= link_to 'Destroy', location, method: :delete, data: { confirm: 'Are you sure?' } %></center></td>
</tr>
</tbody>
Started GET "/locations?_=1492293579724" for ::1 at 2017-04-15 15:59:40 -0600
Processing by LocationsController#index as JS
Parameters: {"_"=>"1492293579724"}
Rendering locations/index.html.erb within layouts/application
Location Load (0.4ms) SELECT "locations".* FROM "locations"
Rendered locations/_location_edit.html.erb (0.5ms)
Rendered collection of locations/_location.html.erb [1 times] (13.1ms)
Rendered locations/_locations_form.html.erb (3.5ms)
Rendered locations/index.html.erb within layouts/application (46.1ms)
Rendered nav/_signed_out.html.erb (2.2ms)
Rendered users/shared/_links.html.erb (0.7ms)
Rendered users/sessions/_user_login.html.erb (16.6ms)
Rendered locations/_location_edit.html.erb (0.1ms)
Rendered collection of locations/_location.html.erb [1 times] (14.4ms)
Rendered locations/_public_locations.html.erb (28.4ms)
Completed 200 OK in 232ms (Views: 218.4ms | ActiveRecord: 0.4ms)
$("#publicLocation").on("click", function() {
$.ajax({
url: '/locations',
method: 'GET'
}).done(function(response){
// Tell the app where to put the response here
// the response contains your partial, which you can append to an empty
// div in your html.
}).fail(function(error){
console.log(error)
})
})