2011-07-26 24 views
0

我有一個小圖庫/圖像應用程序,我試圖將一些Ajax與Jquery結合。我從小處着手,將我的錨定標記變成了ajax。現在,我有一個分頁鏈接和一個鏈接到外部網址的圖片。Rails 3 - 與JQuery的外部錨

現在,我的分頁鏈接正在使用ajax,雖然我的image_link不會打開它的目標網址。我的指數和部分代碼如下:

index.html.erb

<div id="galleries"> 
<%= render 'pictures' %> 
</div> 

和_pictures.html.erb:

<% @galleries.each do |g| %> 
    <% for image in g.images %> 
    <div id="picture"> 
     <%= render 'top_nav'%> 

<!-- this is the link that does not work w/ ajax --> 
     <%= link_to image_tag(image.file_url(:preview)), g.source, :remote => true, :target => "_blank" %> 

     <%= will_paginate(@galleries, :next_label => "Forward", :previous_label => "Previous") %> 
    </div> 

    <br /> 

    <div class="image_description"> 
     <h2> 
     <%= g.title %>, 
     <span class="time"> 
     <%= distance_of_time_in_words(g.created_at, Time.now, include_seconds = false)%> ago 
     </span> 
     </h2> 
    </div>  
    <% end %> 
<% end %> 

我相信我讓AJAX接管兩分頁鏈接和外部圖像鏈接通過使用#picture a,這裏是我的javascript在application.js中:

$(function() { 
    $("#picture a").live("click", function() { 
     $.getScript(this.href); 
     return false; 
    }); 
}); 

我index.js.erb的情況如下:

$("#galleries").html("<%= escape_javascript(render("pictures")) %>"); 

對於破產圖像鏈接,這裏就是在HTML它輸出:

<a target="_blank" data-remote="true" href="http://www.google.com"> 
<img src="/uploads/image/file/3/preview_Screenshot-2.png?1311699598" alt="Preview_screenshot-2"> 
</a> 

但是,當你點擊這個,它什麼都不做。我的猜測是它正在做一些事情,它一遍又一遍地渲染圖片,並且不知道如何鏈接到外部,因爲我已經劫持了link_to的功能,並且我在我的index.js.erb中告訴它只是渲染部分反覆?無論如何,我需要幫助編寫這個外部鏈接作爲ajax鏈接,因爲我最終想調用模型方法來更新選擇/鏈接圖像上的一些數據。

感謝

回答

0

看起來你錯過了:在你的link_to方法符號

<%= link_to "destroy", article_path(@article), :method => :delete, :remote => true %> 

builds 

<a href="/articles/1" data-method="delete" data-remote="true" rel="nofollow">destroy</a> 

Unobtrusive Javascript in Rails 3