0

我有两个 button_to 调用来播放视频。该按钮在我的视频控制器中调用一个特色动作。

在我的控制器中

def featured
 video = Video.find(params[:id]) 
 video.nfeatured = params[:nfeatured]
 video.save
respond_to do |format|
  format.html { redirect_to :back }
  format.js
 end
end

在我看来

<td class="featured">
<% if video.nfeatured == false %>
<%= button_to 'Feature', featured_network_video_path(network, video, 
:nfeatured => true), :remote => true, :class => :feature %>

 <% else %>
 <%= button_to 'Remove', featured_network_video_path(network, video,
 :nfeatured => false), :remote => true, :class => :unfeature  %>
<% end %>
</td>

在成功的 ajax 发布后将按钮更改为“删除”的最不显眼的方法是什么?其他一切工作正常。我尝试在 features.js.erb 文件中进行 jQuery 切换调用,但它不起作用。

4

1 回答 1

1

我喜欢这样做:

<td class="featured">
<%= render :template => "videos/featured" %>
</td>

精选.html.erb:

<% if @video.nfeatured == false %>
  <%= button_to 'Feature', featured_network_video_path(@network, @video, 
  :nfeatured => true), :remote => true, :class => :feature %>

<% else %>
  <%= button_to 'Remove', featured_network_video_path(@network, @video,
  :nfeatured => false), :remote => true, :class => :unfeature  %>
<% end %>

精选.js.erb

$(".featured").html("<%= j(render :template => "videos/featured", :handlers => [:erb]) %>");

是的,我认为这不是最好的方法。我希望看到更正确的解决方案。

编辑:对于循环,此解决方案不适合。第二个版本:

<% unless video.nfeatured %>
  <%= button_to 'Feature', featured_network_video_path(network, video, 
  :nfeatured => true), :remote => true, :class => :feature %>
  <%= button_to 'Remove', featured_network_video_path(network, video,
  :nfeatured => false), :remote => true, :class => 'unfeature hidden'  %>
<% else %>
  <%= button_to 'Feature', featured_network_video_path(network, video, 
  :nfeatured => true), :remote => true, :class => 'feature hidden' %>
  <%= button_to 'Remove', featured_network_video_path(network, video,
  :nfeatured => false), :remote => true, :class => :unfeature  %>
<% end %>

在一些咖啡文件中:

$('.featured').bind "ajax:success", ->
  $(this).toggle()
  $(this).closest('a').toggle()

我不确定这段代码(它显然需要重构),但我希望你能明白。

于 2012-05-20T18:50:17.813 回答