【问题标题】:Rails - redirect to rendering partial from controller actionRails - 从控制器动作重定向到渲染部分
【发布时间】:2013-02-15 02:44:23
【问题描述】:

我正在 Rails 中构建单页应用程序。我在像这样点击按钮的根页面中成功填充了部分;

<div class="span12" style="margin-bottom:20px;">
            <%= link_to "Messages", "/messages", :class=>"btn btn-large btn-primary span12", :remote => true %>
</div> 

这成功地呈现了_index.html.erb 部分。在此页面中有一个消息列表和删除它们的链接。当我删除它们时,我得到一个模板缺失错误,因为它正在寻找 index.html.erb 并在删除消息后重定向到localhost:3000/messages。这是我的销毁操作,在删除消息后被重定向

if @message.destroy
      flash[:notice] = "Message was successfully deleted"
      redirect_to messages_path
else
      flash[:error] = "Message could not be deleted please try again"
      redirect_to messages_path
end

如何让这个动作在localhost:3000/ 中再次渲染_index.html.erb 部分

【问题讨论】:

  • 哪个操作会渲染包含_index.html.erb partial 的视图?您只需重定向到该操作而不是重定向到索引操作。
  • 只有一个视图!它在加载时默认呈现不同的部分。其他部分在按钮点击时呈现

标签: ruby-on-rails-3 controller partial-views


【解决方案1】:

您不应该在每次删除消息时呈现部分索引。它应该只用 javascript 完成。

我认为你可以这样做:

$(".messages").on(".message .destroy-link", "ajax:success", function(data) {
    var $link = $(this);
    $link.closest(".message").remove();
})

并且在您的控制器中,您不会为 format.js 重定向任何地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2012-03-02
    • 2016-02-03
    • 2011-08-28
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    相关资源
    最近更新 更多