我正在尝试创建一个类似系统的评论,用户可以在其中向“决定”添加“结果”。当决策中添加了 1 个时,我喜欢隐藏结果创建表单以“隐藏”。
我最好的猜测是计算结果与决策相关联的实例的数量,并创建一个 if 语句以在结果实例超过 0 时隐藏表单。
我尝试了不同的方法来完成这个,但我似乎无法使计数或 if 语句起作用。我是编码新手,供您引用:-)。
谁能给我建议如何解决这个问题?
我的代码: Controller /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
Controller /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
模型/结果.rb
class Outcome < ActiveRecord::Base
belongs_to :decision
end
模型/决策.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 %>
最佳答案
<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" %>
关于ruby-on-rails - 如何在关联模型只有一个实例时隐藏表单 #rails #beginner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33256455/