How to Show True/False Boolean In Separate Tables

2019-09-17 05:42发布

问题:

I'm able to check off the Boolean in the _form, but then the "goal" only shows in the "accomplished" goals table and not in the goals table above it (regardless of if it is checked off or not).

How can I show false goals (those goals not checked off as completed) in the top table and true goals (those checked off as completed) in the bottom table?

Sorry for the code bomb drop, I'm new to Boolean & scopes so I wanted to make sure I showed all the code that might be useful to solving this problem because I threw in random stuff I thought might work.

index.html.erb

<!-- Default bootstrap panel contents -->
<div id="values" class="panel panel-default">

  <div class="panel-heading"><h4><b>GOALS</b></h4></div>

  <!-- Table -->
  <table>
    <% @goals.each do |goal| %>
      <% if goal.user == current_user %>
      <% if goal.accomplished = false %>
      <tr>
        <td class="value">
        <%= link_to edit_goal_path(goal) do %>
        <%= goal.name %>
        <% end %></td>

        <td class="category">
          <b><%= goal.deadline.strftime("%m-%d-%Y") %></b>
        </td>
      </tr>
      <% end %>
      <% end %>
  <% end %>
 </table>
</div>

  <div class="values-button">
  <%= link_to new_goal_path, class: 'btn'  do %>
  <b><span class="glyphicon glyphicon-plus"</span></b>
  <% end %>
  </div>

<!-- Default bootstrap panel contents -->
<div id="values" class="panel panel-default">

  <div class="panel-heading"><h4><b>ACCOMPLISHED</b></h4></div>

  <!-- Table -->
  <table>
    <% @goals.each do |goal| %>
      <% if goal.user == current_user %>
      <% if goal.accomplished = true %>
      <tr>
        <td class="value">
        <%= link_to edit_goal_path(goal) do %>
        <%= goal.name %>
        <% end %></td>

        <td class="category">
          <b><%= goal.deadline.strftime("%m-%d-%Y") %></b>
        </td>
      </tr>
      <% end %>
      <% end %>
  <% end %>
 </table>
</div>

goal.rb

class Goal < ActiveRecord::Base
    belongs_to :user
    scope :accomplished, -> { where(accomplished: true) }
end

create_goals.rb

class CreateGoals < ActiveRecord::Migration
  def change
    create_table :goals do |t|
      t.string :name
      t.date :deadline
      t.boolean :accomplished

      t.timestamps null: false
    end
  end
end

schema.rb (part of it)

  create_table "goals", force: true do |t|
    t.string   "name"
    t.date     "deadline"
    t.boolean  "accomplished", default: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
  end

  add_index "goals", ["deadline"], name: "index_goals_on_deadline"
  add_index "goals", ["user_id"], name: "index_goals_on_user_id"

_form.html.erb

<%= form_for(@goal) do |f| %>
  <% if @goal.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@goal.errors.count, "error") %> prohibited this goal from being saved:</h2>

      <ul>
      <% @goal.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="america">
<form>

  <div class="form-group">
    <%= f.text_field :name, class: 'form-control', placeholder: 'Enter Goal' %>
  </div>
    <div class="date-group">
      <label> Deadline: </label>
      <%= f.date_select :deadline, :order => [:month, :day, :year], class: 'date-select' %>
    </div>

<div class="america2">
  <%= button_tag(type: 'submit', class: "btn") do %>
  <span class="glyphicon glyphicon-plus"></span>
  <% end %>

  <%= link_to goals_path, class: 'btn' do %>
  <span class="glyphicon glyphicon-chevron-left"></span>
  <% end %>

  <%= link_to @goal, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' do %>
  <span class="glyphicon glyphicon-trash"></span>
  <% end %>

  <%= f.check_box :accomplished, class: 'btn' do %>
  <span class="glyphicon glyphicon-ok"></span>
  <% end %>
</div>
  </form>
</div>
<% end %>

回答1:

in your model create a unaccomplished scope like

scope :unaccomplished, -> { where(accomplished: false) }

then on the controller you can do

class GoalsController < ApplicationController
  def index
    @accomplished_goals = current_user.goals.accomplished
    @unaccomplished_goals = current_user.goals.unaccomplished
  end
end

now finally on the index page you can do

<table>
    <% @accomplished_goals.each do |accomplished| %>
      ....
    <% end %>
</table>

<table>
    <% @unaccomplished_goals.each do |unaccomplished| %>
        ....
    <% end %>
</table>