【问题标题】:What is the easiest way to update a rails button_to after an ajax post?在 ajax 发布后更新 rails button_to 的最简单方法是什么?
【发布时间】:2012-05-20 18:34:14
【问题描述】:

我有两个 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 切换调用,但它不起作用。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 jquery ujs


    【解决方案1】:

    我喜欢这样做:

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

    featured.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 %>
    

    featured.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()
    

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

    【讨论】:

    • 为什么不用j 而不是escape_javascript?前者不是更容易记住吗?
    • 非常感谢您的回答...我在回调中收到 500 个服务器错误。它成功保存了操作,但没有正确呈现视图。有什么想法吗?我还必须传入局部变量,因为这个渲染是在一个块内。
    • @jdoe,谢谢,我之前只是不知道)@Kyle C,对不起,我忘记了您必须传递本地参数或使用实例变量。我更喜欢第二种解决方案。您有一个错误可能是因为您没有在featured 操作中定义变量@network@video。查看更多日志错误,它有很大帮助。我编辑了答案。
    • 还是不行,我在特色动作中添加了网络和视频实例变量。渲染在 for 循环中被调用,所以我尝试将这些变量传递给渲染并获取 ActionView::Template::Error(堆栈级别太深):actionpack (3.2.1) lib/action_dispatch/http/mime_type。 RB:219。这是被称为 的循环
    • @Kyle C,我已经更新了答案,类似的解决方案应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    • 2019-04-01
    相关资源
    最近更新 更多