问题描述
我可以检查_form中的布尔值,但是目标仅显示在已完成的目标表中,而不显示在上方的目标表中(无论是否已检查) 。
如何在顶部表格中显示虚假目标(未完成的目标)以及底部表格中的真正目标(已完成检查的目标)?
很抱歉代码炸弹丢失,我是布尔&因此我想确保显示了可能对解决此问题有用的所有代码,因为我随机扔了一些我认为可能有用的东西。
$ b
索引.html.erb
<! - 默认启动面板内容 - >
< div id =valuesclass =panel panel-default>
< div class =panel-heading>< h4>< b> GOALS< / b>< / h4>< / div>
<! - 表格 - >
< table>
< tr>
< td class =value>
<%= 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>
< b>< span class =glyphicon glyphicon-plus< / span>< / b>
<%end%>
< / div>
< div id =valuesclass =panel panel-default>
< div class =panel-heading>< h4>< b> ACCOMPLISHED< / b>< / h4>< / div>
<! - 表格 - >
< table>
< tr>
< td class =value>
<%= 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:已完成, - > {where(done:true)}
end
create_goals.rb
class CreateGoals< ActiveRecord :: Migration
def change
create_table:goals do | t |
t.string:name
t.date:截止日期
t.boolean:已完成
t.timestamps null:false
结束
结束
结束
schema.rb(其中的一部分)
create_tablegoals,force:true do | t |
t.stringname
t.date截止日期
t.boolean已完成,默认值:false
t.datetimecreated_at,null:false
t.datetimeupdated_at,null:false
t.integeruser_id
结束
add_index目标,[截止时间],名称:index_goals_on_deadline
add_index目标,[user_id],名称:index_goals_on_user_id
_form.html.erb
<%= form_for(@goal)do | f | %GT;
< div id =error_explanation>
< h2><%= pluralize(@ goal.errors.count,error)%>禁止保存此目标:< / h2>
< ul>
< li><%= message%>< / li>
<%end%>
< / ul>
< / div>
<%end%>
< div class =america>
< form>
< div class =form-group>
< / div>
< div class =date-group>
< label>截止日期:< / label>
< / div>
< div class =america2>
<%= button_tag(类型:'submit',class:btn)do%>
< span class =glyphicon glyphicon-plus>< / span>
<%end%>
< span class =glyphicon glyphicon-chevron-left>< / span>
<%end%>
< span class =glyphicon glyphicon-trash>< / span>
<%end%>
< span class =glyphicon glyphicon-ok>< / span>
<%end%>
< / div>
< / form>
< / div>
<%end%>
在您的模型中创建一个未完成的范围,如
scope:unaccomplished, - > {where(done:false)}
然后在控制器上执行
class GoalsController< ApplicationController
def index
@accomplished_goals = current_user.goals.accomplished
@unaccomplished_goals = current_user.goals.unaccomplished
end
end
$ c $现在终于可以在索引页面上完成了。<表>
<%@ done_goals.each do |已完成| %GT;
....
<%end%>
< / table>
< table>
<%@ unaccomplished_goals.each do | unaccomplished | %GT;
....
<%end%>
< / table>
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 %>
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>
这篇关于如何在同一索引页的单独表格中显示真/假布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!