首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当关联模型#rails #初学者有一个实例时,如何隐藏表单

当关联模型#rails #初学者有一个实例时,如何隐藏表单
EN

Stack Overflow用户
提问于 2015-10-21 10:07:20
回答 2查看 57关注 0票数 2

我正在尝试创建一个类似于系统的评论,用户可以在其中添加一个“结果”到“决策”中。我喜欢隐藏结果创建表单,以‘隐藏’时,有一个添加到决策。

要做到这一点,我的最佳猜测是计算决策结果的关联实例的数量,并创建一个if语句,用于在结果实例超过0时隐藏表单。

我尝试了不同的方法来完成这个任务,但我似乎无法让计数或if语句起作用。我不熟悉编码,供你参考:-)。

有人能给我一个如何解决这个问题的建议吗?

我的代码:控制器/决策控制器. My

代码语言:javascript
复制
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

控制器/结果控制器

代码语言:javascript
复制
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

模型/结果

代码语言:javascript
复制
class Outcome < ActiveRecord::Base
  belongs_to :decision
end

模型/决定。

代码语言:javascript
复制
class Decision < ActiveRecord::Base
  has_many :outcomes
end

决定/show.html.erb

代码语言:javascript
复制
    <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

代码语言:javascript
复制
<%= 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

代码语言:javascript
复制
<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 %>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-21 10:18:18

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2015-10-21 10:46:23

听起来像标准的条件逻辑:

代码语言:javascript
复制
#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时隐藏表单。

当然-这叫条件逻辑

在您的示例中,您正在寻找几种可能的方法(用于确定关联数据的大小):

  • 长度 (Pardeep的答案) --查询数组的长度
  • 大小 -调用count (SQL查询)或length
  • 计数 --执行db查询(只应在没有加载数据的情况下使用)
  • 有吗? --非常类似于长度,但根据是否存在项返回true/false

好参考:ActiveRecord:大小与计数

  • 如果您已经加载了所有条目,比如User.all,那么您应该使用length来避免另一个db查询。
  • 如果没有加载任何内容,请使用count对数据库进行count查询
  • 如果您不想费心考虑这些问题,请使用size,它将适应

我使用.any?是因为它在这个例子中是最简洁的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33256455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档