如何在同一索引页的不同表中显示 True/False 布尔值?

How to Show True/False Boolean In Separate Tables on the Same Index Page?

我可以检查 _form 中的布尔值,但是 "goal" 只显示在 "accomplished" 目标 table 而不是目标 table 在它上面(不管它是否被勾选)。

如何在顶部 table 显示虚假目标(未勾选完成的目标),在底部 table 显示真实目标(已完成勾选的目标)?

对于代码炸弹投掷表示抱歉,我是布尔值和作用域的新手,所以我想确保我展示了所有可能对解决此问题有用的代码,因为我随机添加了我认为可能有用的东西。

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(部分)

  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 %>

在您的模型中创建一个未完成的范围,例如

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

然后在控制器上你可以做

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

现在终于可以在索引页上做

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

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