2013-07-11 26 views
0

嗨,大家好我試圖使用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看到阿賈克斯它獲取數據和替換它但仍不知道如何顯示它。

Adding a new record New record saved and displayed immediately Chrome's debugging tool

回答

1

有一個ID爲「# 「在replaceWith選擇器中。刪除「#」並再次嘗試$('table.table tbody')或將id =「allsites」分配給你的tbody,並保持你的jquery選擇器與第一篇文章相同。

1
  • 我會建議你刪除表內的一個額外的div並添加tbodythead爲造型的目的,像這樣:

Index.html.erb

<%= link_to 'New Location', new_location_path, id:"new_link", remote: true%> 
<table class="table table-hover"> 
    <thead> 
    <tr> 
     <th>Name</th> 
     <th>Address</th> 
     <th></th> 
     <th></th> 
     <th></th> 
    </tr> 
    </thead> 
    <tbody> 
     <%= render @locations%> 
    </tbody> 
</table> 
  • 我可能會刪除前插replaceWith

create.js.erb更換

$('#new_location').remove(); 
$('#new_link').show(); 
$('#table.table tbody').replaceWith('<%= escape_javascript(render(@location)).html_safe %>'); 
+0

您好感謝您的回覆,我做了那些你提出修改建議,但仍然沒有顯示沒有刷新數據,怎麼樣'_location.html。 erb'部分?它有什麼問題嗎? – Guille

0

感謝所有人,幾天前我看到了一個在rails上使用集合的例子,因此,我在create_js.erb文件中使用了這一行文件$('table.table tbody').prepend('<%= escape_javascript(render(:partial => "location", :locals => { :location => @location })) %>');,然後在索引中添加了這一行。 html.erb文件<%= render :partial => 'locations/location', :collection => @locations %>我想如果有人可以解釋我這個,是的,它解決了我的問題,但會更好地學習一點點。複製和粘貼不知道什麼被拷入是不是一個好的做法

相關問題