2016-11-18 46 views
1

我有一個應用程序,我正在讓用戶可以發佈項目,然後他們可以選擇「live」,當他們選擇「live」爲true時,該帖子應該顯示在活動選項卡中。不顯示布爾爲真的帖子

現在這是我的posts_controller.rb創建和更新操作

def create 
 
    @post = Post.new(post_params) 
 
    if @post.save 
 
     redirect_to post_path(@post) 
 
     flash[:success]="Post created" 
 
    else 
 
     render 'new' 
 
    end 
 
    end 
 

 
def update 
 
    @post = Post.find(params[:id]) 
 
    if @post.update(post_params) 
 
    flash[:success]="Updated successfully" 
 
    redirect_to post_path(@post) 
 
    else 
 
    render 'edit' 
 
    end 
 
end 
 

 
-------------------------------- 
 
    def post_params 
 
    params.require(:post).permit(:title, :description, 
 
         :category_id, :subcategory_id, :live) 
 
    end

我的帖子的形式進行編輯並創建一個新的職位。

<script type="text/javascript"> 
 
$(document).ready(function() { 
 
    var subcat; 
 
    subcat = $('#subcategory-select').html(); 
 
    return $('#category-select').change(function() { 
 
    var cat, options; 
 
    cat = jQuery('#category-select').children('option').filter(':selected').text(); 
 
    options = $(subcat).filter("optgroup[label='" + cat + "']").html(); 
 
    if (options) { 
 
     return $('#subcategory-select').html(options); 
 
    } else { 
 
     return $('#subcategory-select').empty(); 
 
    } 
 
    }); 
 
}); 
 
</script> 
 
<script> 
 
    $(function() { 
 
    $("#tabs").tabs(); 
 
    }); 
 
</script> 
 
<%= render 'shared/errors', obj: @post%> 
 
<%= form_for @post do |f| %> 
 
    <p> 
 
\t <%= f.label :title %><br/> 
 
\t <%= f.text_field :title, maxlength: "10" %> 
 
    </p> 
 
    <p> 
 
\t <%= f.label :description %><br> 
 
\t <%= f.text_area :description %> 
 
    </p> 
 
    <p> 
 
\t <%= f.label :category_id%> 
 
\t <%= f.collection_select(:category_id, Category.all, :id, :name,  
 
\t    { prompt: 'Select a category' }, { id: 'category-select' }) %> 
 
    </p> 
 
    <p> 
 
\t <%= f.label :subcategory_id%> 
 
\t <%= f.grouped_collection_select :subcategory_id, Category.all, :sub_categories, 
 
\t   :name, :id, :name, { include_blank: 'Select a sub category' }, 
 
\t            { id: 'subcategory-select' } %> 
 
    </p> 
 
    <p> 
 
\t <%= f.label :live%> 
 
\t <%= check_box_tag :live , 0 , @post.live ? false : true %> <!--work on--> 
 
    <p> 
 
    <p> 
 
\t <%= f.submit %> 
 
    </p> 
 

 
<% end %> 
 

 
<%= link_to "Back to posts listing", posts_path %>

的帖子不保存爲live,我可以強制它與然而鐵軌控制檯是活的。這讓我想到下一個問題。

我強制要求post擁有live == true,並且它不會顯示在我的引導程序實時選項卡中。這是我的子類別顯示操作,這是我嘗試首先執行操作的地方。

def show 
 
    @subcategory = SubCategory.find(params[:id]) 
 
    @subcategory_posts = @subcategory.posts#live posts in this subcat 
 
    @subcategory_posts_live = (params[:live] == 'true') 
 
    end

而且show.html.erb引導導航選項卡應顯示實時的職位,但不會對點擊

\t <ul class="nav nav-tabs" role="tablist"> 
 
\t  <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Posts</a></li> 
 
\t  <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Live ◯</a></li> 
 
\t  <li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">xx</a></li> 
 
\t </ul> 
 

 
\t <!-- Tab panes --> 
 
\t <div class="tab-content"> 
 
\t  <div role="tabpanel" class="tab-pane active" id="home"><%=render 'posts/post', obj: @subcategory_posts %></div> 
 
\t  <div role="tabpanel" class="tab-pane" id="profile"><%=render 'posts/post', obj: @subcategory_posts_live %></div> 
 
\t  <div role="tabpanel" class="tab-pane" id="messages">...</div> 
 
\t </div>

我知道,它正在尋找因爲在視圖中顯示<p>No Listings found</p>這是我在'posts/_post`部分顯示的內容,如果在該特定類別中沒有發現任何帖子,例如Live Subcategory帖子。

不確定如何工作這一個,任何指導讚賞。

回答

1

它看起來像你的表格缺少一個複選框,以允許用戶選擇一個帖子是否是活的。你需要一個複選框。

所以這樣的事情可能工作:

<%= f.check_box :live %> 

要查詢是現場所有文章(我假設這是你的數據庫的布爾列):

@live_posts = Post.where(live: true) 

爲了您的子類別查詢,你可以這樣做:

@subcategory_posts = Post.where(subcategory_id: params[:id], live: true) 

(這是假設的帖子有subcategory_id列)

+1

嗨,賴安,謝謝你的答案!原來我忘了,但在我的_form部分f.check_box,我用另一個答案的代碼不是這麼簡單,非常感謝。新帖子保存爲#<帖子ID:8,標題:「我們活着嗎?」,描述:「向賴安多爾蒂歡呼」,category_id:1,subcategory_id:1,live:true,created_at:「2016-11-18 00 :57:40「,updated_at:」2016-11-18 00:57:40「>]>引導程序導航選項卡是否自動ajax,還是必須在? – Jack

+0

太棒了,很高興聽到它的工作!我對bootstrap選項卡沒有太多的瞭解,所以我不能真正幫助你,對不起! –