[英]Create a partials path in rails
我正在使用郵箱管理器gem允許在我的應用程序上發送消息。
我希望將顯示收件箱,已發送,已刪除郵件的頁面顯示在我的單個頁面上,而不是使用不得不轉到另一個頁面來查看此頁面。
為此,我將部分設置如下:
的index.html
<%= render :partial => 'conversations/index', :locals => {:box => @box } %>
效果很好,我看到了收件箱,發送和回收站鏈接。
但是,當我單擊收件箱時,它將帶我進入包含收件箱,發送和回收站的另一頁。 這是原始的對話/index.html.erb(而不是_index)。 原因是因為_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>
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
正是conversations_path使我回到了sessions / index.html,而不是讓我呆在部分內容中。
所以問題是,我該如何更改sessions_path以使我停留在部分路徑內(刪除路徑無濟於事)
有一種方法可以通過異步加載框的渲染部分(您需要jQuery)來做您想要的(我認為)。
在您的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>
在您的控制器中:
def show
@conversations = Conversation.where(box: params[:box]) # or whatever your do
render partial: 'conversations/conversation', collection: @conversations
end
在您的助手中:
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
這對您來說合適嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.