我正在尝试创建一个类似系统的评论,用户可以在“决策”中添加“结果”。我希望隐藏结果创建表单,以便在决定添加1时隐藏。
我最好的做法是将结果实例的数量计算到决策中,并在结果超过0的情况下创建一个隐藏表单的if语句。
我尝试了不同的方法来实现这一点,但我似乎无法使计数或if语句起作用。我是编码供您参考的新手: - )。
有人可以给我一个如何解决这个问题的建议吗?
我的代码: 控制器/ decisions_controller.rb
class DecisionsController < ApplicationController
before_action :find_decision, only: [:show, :edit, :update, :destroy]
def index
# gets all rows from decision table and puts it in @decision variable
@decisions = Decision.all
end
def show
# find only the decision entry that has the id defined in params[:id]
end
# shows the form for creating a entry
def new
@decision = Decision.new
end
# creates the entry
def create
@decision = Decision.new(decision_params)
if @decision.save
redirect_to @decision
else
render 'new'
end
end
# shows the form for editing a entry
def edit
end
# updates the entry
def update
if @decision.update(decision_params)
redirect_to @decision
else
render 'edit'
end
end
def destroy
@decision.destroy
redirect_to root_path
end
private
def find_decision
# Loads the right entry.
@decision = Decision.find(params["id"])
end
def decision_params
params.require(:decision).permit(:title, :forecast, :review_date)
end
end
控制器/ outcomes_controller.rb
class OutcomesController < ApplicationController
def create
@decision = Decision.find(params[:decision_id])
@outcome = @decision.outcomes.create(params[:outcome].permit(:actual, :strength, :weakness))
redirect_to decision_path(@decision)
end
end
模型/ outcome.rb
class Outcome < ActiveRecord::Base
belongs_to :decision
end
模型/ decision.rb
class Decision < ActiveRecord::Base
has_many :outcomes
end
决定/ show.html.erb
<h1>Decision showpage</h1>
<h2><%= @decision.title %></h2>
<p><%= @decision.created_at %></p>
<p><%= @decision.forecast %></p>
<p><%= @decision.review_date %></p>
<%= render @decision.outcomes %>
<%= link_to "Delete Decision", decision_path(@decision), method: :delete, data: { confirm: "Are you sure?" } %>
<%= render "outcomes/form" %>
<%= render "outcomes/outcome" %>
结果/ _form.html.erb
<%= form_for([@decision, @decision.outcomes.build]) do |f| %>
<%= f.label :actual %>:
<%= f.text_field :actual %> <br/>
<%= f.label :strength %>:
<%= f.text_area :strength %> <br/>
<%= f.label :weakness %>:
<%= f.text_area :weakness %> <br/>
<%= f.submit %>
<% end %>
结果/ _outcome.html.erb
<h2>outcome</h2>
<%= @decision.outcomes.first.actual %> </br>
<h3>What i found i'm good at</h3>
<%= @decision.outcomes.first.strength %> </br>
<h3>What i found i'm weak at</h3>
<%= @decision.outcomes.first.weakness %>
答案 0 :(得分:1)
<h1>Decision showpage</h1>
<h2><%= @decision.title %></h2>
<p><%= @decision.created_at %></p>
<p><%= @decision.forecast %></p>
<p><%= @decision.review_date %></p>
<%= render @decision.outcomes %>
<%= link_to "Delete Decision", decision_path(@decision), method: :delete, data: { confirm: "Are you sure?" } %>
<%if @decision.outcomes.length < 1 %>
<%= render "outcomes/form" %>
<%end%>
<%= render "outcomes/outcome" %>
答案 1 :(得分:0)
听起来像标准的条件逻辑:
#app/views/decisions/show.html.erb
<h1>Decision</h1>
<h2><%= @decision.title %></h2>
<% %w(created_at forecast review_date).each do |option| %>
<%= content_tag :p, @decision.send(option) %>
<% end %>
<% if @decision.outcomes.any? %>
<%= render @decision.outcomes %>
<%= render "outcomes/form" %>
<% end %>
<%= link_to "Delete Decision", decision_path(@decision), method: :delete, data: { confirm: "Are you sure?" } %>
我最好的做法是将结果的关联实例数量计算为决策,创建一个if语句,以便在结果超过0的情况下隐藏表单。
当然 - 它被称为conditional logic
在您的情况下,您正在寻找几种可能的方法(以确定相关数据的大小):
Pardeep
的答案) - 查询数组的长度count
(SQL查询)或length
这里好参考:ActiveRecord: size vs count
如果您已加载所有条目,请说
User.all
,那么您应该使用length
以避免另一个数据库查询如果您还没有加载任何内容,请使用count来进行
count
查询 你的数据库如果您不想打扰这些注意事项,请使用
size
将适应
我使用了.any?
因为它在这个例子中最简洁。