0

我在视图/用户/显示中有此链接:

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

现在,当我单击上面的链接时,我想将 [1..6] 更改为 [1..-1] 而不在视图/用户/显示中重新渲染:

<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>

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

4

1 回答 1

1

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

控制器:

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("<%= escape_javascript(render(:partial => 'users/show', :object => @photos)) %>");

如果您使用原型,则必须在控制器中进行。

于 2013-04-02T05:08:35.893 回答