2016-11-18 43 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选项卡没有太多的了解,所以我不能真正帮助你,对不起! –