Jack Jack - 1 year ago 57
Javascript Question

Not showing posts where boolean is true

I have an app I am making where users can post items and then they can select "live" when they select "live" as true the post should show up in a live tab.

Right now this is my posts_controller.rb create and update actions

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

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

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

My Post form for editing and creating a new post.

<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();
$(function() {
$( "#tabs" ).tabs();
<%= render 'shared/errors', obj: @post%>
<%= form_for @post do |f| %>
<%= f.label :title %><br/>
<%= f.text_field :title, maxlength: "10" %>
<%= f.label :description %><br>
<%= f.text_area :description %>
<%= f.label :category_id%>
<%= f.collection_select(:category_id, Category.all, :id, :name,
{ prompt: 'Select a category' }, { id: 'category-select' }) %>
<%= f.label :subcategory_id%>
<%= f.grouped_collection_select :subcategory_id, Category.all, :sub_categories,
:name, :id, :name, { include_blank: 'Select a sub category' },
{ id: 'subcategory-select' } %>
<%= f.label :live%>
<%= check_box_tag :live , 0 , @post.live ? false : true %> <!--work on-->
<%= f.submit %>

<% end %>

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

The posts doesn't save as being
, I can force it into being live with the rails console however. This brings me to the next issue.

I forced a
to have
live == true
and it doesn't show up in my bootstrap live tab. This is for my subcategory show action which is where I am trying to do this first.

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

And the
bootstrap navigation tabs which should show the live posts but doesn't on click

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

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

I know that it is looking for posts because in the view it shows
<p>No Listings found</p>
which is what I am displaying in my 'posts/_post` partial if no posts are found within that specific category i.e Live Subcategory Posts.

Unsure on how to work this one out, any guidance appreciated.

Answer Source

It looks like your form is missing a checkbox to allow the user to select if a post is live or not. You need a checkbox.

So something like this may work:

<%= f.check_box :live %>

To query for all posts that are live (I'm assuming this is a boolean column in your db):

@live_posts = Post.where(live: true)

For your subcategory query, you can do this:

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

(This assumes posts have a subcategory_id column)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download