Javascript 在link_上单击“用ajax替换节内容”

Javascript 在link_上单击“用ajax替换节内容”,javascript,jquery,ruby-on-rails,ajax,link-to,Javascript,Jquery,Ruby On Rails,Ajax,Link To,我在view/users/show中有此链接: <%= link_to "Photos (#{@user.photos.count})", user_path(@user), id: "photo_link", remote: true %> <section id="photo_galery"> <% unless @user.photos[0].nil? %> <% for photo in @user.photos[1..6] %&g

我在view/users/show中有此链接:

<%= link_to "Photos (#{@user.photos.count})", user_path(@user), id: "photo_link", remote: true %>
<section id="photo_galery">
  <% unless @user.photos[0].nil? %>
    <% for photo in @user.photos[1..6] %>
      <%= link_to image_tag(photo.photo.url(:subprofile_thumbnail), id: "subpics"), photo.photo.url(:original), id: "subprofile_photos", class: "fancybox", rel: "gallery01" %>
    <% end %>
  <% end %>
</section>

现在,当我单击上面的链接时,我想将[1..6]更改为[1..-1],而无需在view/users/show中重新提交:

<%= link_to "Photos (#{@user.photos.count})", user_path(@user), id: "photo_link", remote: true %>
<section id="photo_galery">
  <% unless @user.photos[0].nil? %>
    <% for photo in @user.photos[1..6] %>
      <%= link_to image_tag(photo.photo.url(:subprofile_thumbnail), id: "subpics"), photo.photo.url(:original), id: "subprofile_photos", class: "fancybox", rel: "gallery01" %>
    <% end %>
  <% end %>
</section>


我怎样才能做到这一点?谢谢

您必须从控制器端进行变量更改

控制器:

def show
  @user = current_user
  if request.xhr? 
    @photos = @user.photos[1..-1]
  else
    @photos = @user.photos[1..6]
  end 
end
视图: show.html.erb

<section id="photo_galery">
  <%= render "photos"%>
</section>
<%= link_to "Photos (#{@user.photos.count})", user_path(@user), id: "photo_link", remote: true %>

_photos.html.erb

<% unless @user.photos[0].nil? %>
    <% for photo in @photos %>
      <%= link_to image_tag(photo.photo.url(:subprofile_thumbnail), id: "subpics"), photo.photo.url(:original), id: "subprofile_photos", class: "fancybox", rel: "gallery01" %>
    <% end %>
  <% end %>

show.js.erb(如果使用jquery)

$(“#photo_galery”).html(“'users/show',:object=>@photos))%>”;
如果您使用的是prototype,则必须在controller中完成