如何在同一索引页上的单独表中显示真/假布尔值?
我能够在_form中检查布尔值,但是“目标”只显示在“已完成”目标表中,而不是在其上方的目标表中(无论是否已检查)。
如何在顶部表格中显示虚假目标(未完成核对的目标)和底部表格中的真实目标(已完成核对的目标)?
对于代码炸弹丢弃感到抱歉,我是布尔和范围的新手,所以我想确保我展示了解决这个问题可能有用的所有代码,因为我投入了我认为可能有用的随机内容。
index.html.erb
GOALS
ACCOMPLISHED
goal.rb
class Goal { 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
prohibited this goal from being saved:
在你的模型中创建一个未完成的范围,如
scope :unaccomplished, -> { where(accomplished: false) }
然后在控制器上你可以做
class GoalsController < ApplicationController def index @accomplished_goals = current_user.goals.accomplished @unaccomplished_goals = current_user.goals.unaccomplished end end
现在终于可以在索引页面上做了
<% @accomplished_goals.each do |accomplished| %> .... <% end %>
<% @unaccomplished_goals.each do |unaccomplished| %> .... <% end %>