简体   繁体   English

在Rails中创建局部路径

[英]Create a partials path in rails

Im using the mailboxer gem to allow message sending on my app. 我正在使用邮箱管理器gem允许在我的应用程序上发送消息。

I would like the page showing inbox,sent, trash to show on my single page rather than uses having to go to another page to see this. 我希望将显示收件箱,已发送,已删除邮件的页面显示在我的单个页面上,而不是使用不得不转到另一个页面来查看此页面。

In order to do this I have set up a partial as follows: 为此,我将部分设置如下:

index.html 的index.html

<%= render :partial => 'conversations/index', :locals => {:box => @box } %>

That works fine and I see the inbox,send and trash links. 效果很好,我看到了收件箱,发送和回收站链接。

However when I click on inbox it takes me to another page with inbox, send and trash. 但是,当我单击收件箱时,它将带我进入包含收件箱,发送和回收站的另一页。 This is the original conversations/index.html.erb (as opposed to _index). 这是原始的对话/index.html.erb(而不是_index)。 The reason is because the _index code is as follows: 原因是因为_index代码如下:

<div class="row">
  <div class="col-sm-3">
    <ul class="nav nav-pills nav-stacked">
      <%= mailbox_section 'inbox', @box %>
      <%= mailbox_section 'sent', @box %>
      <%= mailbox_section 'trash', @box %>
    </ul>
  </div>
  <div class="col-sm-9">
    <ul class="list-group">
      <%= render partial: 'conversations/conversation', collection: @conversations %>
    </ul>
  </div>
</div>

the mailbox_section is a method defined in a helper called conversations_helper.rb which is as follows: mailbox_section是在名为sessions_helper.rb的帮助器中定义的方法,如下所示:

module ConversationsHelper
  def mailbox_section(title, current_box, opts = {})
    opts[:class] = opts.fetch(:class, '')
    opts[:class] += ' active' if title.downcase == current_box
    content_tag :li, link_to(title.capitalize, conversations_path(box: title.downcase)), opts
  end
end

it is the conversations_path that is taking me back to the conversations/index.html rather than letting me stay in the partial. 正是conversations_path使我回到了sessions / index.html,而不是让我呆在部分内容中。

So the question is, how do I change the conversations_path so that I stay within the partial (removing the path doesn't help) 所以问题是,我该如何更改sessions_path以使我停留在部分路径内(删除路径无济于事)

There is a way of doing what (I think) you want by asynchronously loading the rendered partials of your boxes (you'll need jQuery). 有一种方法可以通过异步加载框的渲染部分(您需要jQuery)来做您想要的(我认为)。

In your HTML, 在您的HTML中,

<div class="row">
  <div class="col-sm-3">
    <ul id="section-selector" class="nav nav-pills nav-stacked">
      <%= mailbox_section 'inbox', @box %>
      <%= mailbox_section 'sent', @box %>
      <%= mailbox_section 'trash', @box %>
    </ul>
  </div>
  <div class="col-sm-9">
    <ul id="conversations" class="list-group">
      <%= render partial: 'conversations/conversation', collection: @conversations %>
    </ul>
  </div>
</div>

<script type="text/javascript">
  // detect click on your mailbox links
  ('#section-selector li').click(function() {
    // extract the box type
    var box = $(this).data('box');

    // ask the server for the rendered partial of this box
    $.ajax({
      url: "/conversations/" + box,
      cache: false,
      success: function(html){
        // change the content of the conversations ul with the new rendered
        // partial
        $("#conversations").html(html);
      }
    });
  });
</script>

In your Controller: 在您的控制器中:

def show
  @conversations = Conversation.where(box: params[:box]) # or whatever your do
  render partial: 'conversations/conversation', collection: @conversations
end

In your Helper: 在您的助手中:

module ConversationsHelper
  def mailbox_section(title, current_box, opts = {})
    opts[:class] = opts.fetch(:class, '')
    opts[:class] += ' active' if title.downcase == current_box
    opts[:data] = {box: title.downcase} # add the box type to your link
    content_tag :li, link_to(title.capitalize, '#'), opts
  end
end

Does this seem right to you ? 这对您来说合适吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM