我在view / users / show中有这个链接:
<%= link_to "Photos (#{@user.photos.count})", user_path(@user), id: "photo_link", remote: true %>
现在,当我点击上面的链接时,我想将[1..6]更改为[1 ..- 1]而无需在视图/ users / show中重新渲染:
<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>
我怎样才能做到这一点?谢谢!
答案 0 :(得分: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)) %>");
如果您使用原型,则必须在控制器中执行。