嗨,大家好我試圖使用AJAX和JQuery在表格中顯示數據,就像我在代碼中看到的那樣,我使用了部分表單來檢索數據(這個工作很棒),然後點擊保存按鈕時,數據顯示時不刷新整個頁面,但我不知道如何顯示它在一張桌子裏面也是一個jQuery UI滑動效果,對不起,我是新來的rails,甚至更糟糕的web開發:D哪個是使用Ajax和JQuery在表格上顯示數據的最佳方式在rails上使用ruby?
讓我告訴你我的代碼。
create.js.erb
$('#new_location').remove();
$('#new_link').show();
$('#allsites').prepend('<%= escape_javascript(render(@location)).html_safe %>');
_location.html.erb部分文件
<tr>
<td><%= location.name %></td>
<td><%= location.address%></td>
<td><%= link_to 'Show', location %></td>
<td><%= link_to 'Edit', edit_location_path(location) %></td>
<td><%= link_to 'Destroy', location, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
Index.html.erb
<div class="hero-unit">
<%= gmaps4rails(@json) %>
</div>
<%= link_to 'New Location', new_location_path, id:"new_link", remote: true%>
<table class="table table-hover">
<tr>
<th>Name</th>
<th>Address</th>
<th></th>
<th></th>
<th></th>
</tr>
<div id="allsites"><%= render @locations%></div>
</table>
new.js.erb
$('#new_link').hide().after('<%= j render("form") %>');
location_controller.rb
class LocationsController < ApplicationController
def index
@locations = Location.all
@json = Location.all.to_gmaps4rails
end
def new
@location = Location.new
end
def edit
@location = Location.find(params[:id])
end
def create
#@location = Location.new(params[:location])
@location = Location.create(params[:location])
respond_to do |format|
format.html { redirect_to @location, notice: 'Location was successfully created.' }
format.js
end
end
def show
@location = Location.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @location }
end
end
def update
@location = Location.find(params[:id])
respond_to do |format|
if @location.update_attributes(params[:location])
format.html { redirect_to @location, notice: 'Location was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @location.errors, status: :unprocessable_entity }
end
end
end
def destroy
@location = Location.find(params[:id])
@location.destroy
respond_to do |format|
format.html { redirect_to locations_url }
format.json { head :no_content }
end
end
end
正如你可以在secreenshots看到阿賈克斯它獲取數據和替換它但仍不知道如何顯示它。
您好感謝您的回覆,我做了那些你提出修改建議,但仍然沒有顯示沒有刷新數據,怎麼樣'_location.html。 erb'部分?它有什麼問題嗎? – Guille