【问题标题】:How to use nested_attribute boolean to change font color in sidebar?如何使用nested_attribute boolean 更改侧边栏中的字体颜色?
【发布时间】:2015-04-09 19:52:26
【问题描述】:

当用户在_form 中勾选:good 时,我们如何在侧边栏中将该结果的字体颜色呈现为绿色?

我们在索引中完成了这项工作:How to Change Font Color based on Conditional?

但是当我尝试这个时,边栏逻辑通过为每个结果呈现额外的量化/结果来使这个变得更加棘手(我们试图在此处进一步解决这个问题:How to stop double rendering in sidebar?):

<% @averaged_quantifieds.each do |averaged| %>
  <% averaged.results.each do |result| %>
    <% if result.good == true %>
      <div class="green">
        <li>
          <%= raw averaged.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %><%= link_to edit_quantified_path(averaged) do %> <%= averaged.results.first.result_value %> <%= averaged.metric %> <span class="label label-info"> <%= averaged.results.first.date_value.strftime("%b") %></span><% end %>
        </li>
    <% else %>
      <div class="red">
        <li>
          etc...

当前代码:

<% @averaged_quantifieds.each do |averaged| %>
  <li>
    <%= raw averaged.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %><%= link_to edit_quantified_path(averaged) do %> <%= averaged.results.first.result_value %> <%= averaged.metric %> <span class="label label-info"> <%= averaged.results.first.date_value.strftime("%b") %></span><% end %>
  </li>
<% end %>

<% @instance_quantifieds.each do |instance| %>
  <li>
    <%= instance.results.first.date_value.strftime("%b %d") %>: <%= link_to edit_quantified_path(instance) do %> <%= instance.results.first.result_value %> <%= instance.metric %>, <%= raw instance.tag_list.map { |t| link_to t.titleize, tag_path(t) }.join(', ') %><% end %>
  </li>
<% end %>

application_controller

def set_stats
  @quantifieds = Quantified.joins(:results).all
  @averaged_quantifieds = current_user.quantifieds.averaged if current_user
  @instance_quantifieds = current_user.quantifieds.instance if current_user
  @statsresults = current_user.results.stats if current_user
end

更新

quantified.rb

class Quantified < ActiveRecord::Base
	belongs_to :user
 	has_many :results #correct
	has_many :comments, as: :commentable
	accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true #correct
 	scope :averaged,  -> { where(categories: 'Averaged') }
 	scope :instance,  -> { where(categories: 'Instance') }
	scope :private_submit, -> { where(private_submit: true) }
	scope :public_submit, -> { where(private_submit: false) }
 	validates :categories, :metric, presence: true
	acts_as_taggable

	CATEGORIES = ['Averaged', 'Instance']
end

量化控制器

class QuantifiedsController < ApplicationController
  before_action :set_quantified, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:create, :destroy]

  def index
    if params[:tag]
      @quantifieds = Quantified.tagged_with(params[:tag])
    else
      @quantifieds = Quantified.joins(:results).all
      @averaged_quantifieds = current_user.quantifieds.averaged
      @instance_quantifieds = current_user.quantifieds.instance
    end
  end

  def show
  end

  def new
    @quantified = current_user.quantifieds.build 
  end

  def edit
  end

  def create
    @quantified = current_user.quantifieds.build(quantified_params)
    if @quantified.save
      redirect_to quantifieds_url, notice: 'Quantified was successfully created'
    else
      @feed_items = []
      render 'pages/home'
  end
end

  def update
    if @quantified.update(quantified_params)
      redirect_to quantifieds_url, notice: 'Goal was successfully updated'
    else
      render action: 'edit'
  end
end

  def destroy
    @quantified.destroy
    redirect_to quantifieds_url
  end

  private
    def set_quantified
      @quantified = Quantified.find(params[:id])
    end

    def correct_user
      @quantified = current_user.quantifieds.find_by(id: params[:id])
      redirect_to quantifieds_path, notice: "Not authorized to edit this goal" if @quantified.nil?
    end

    def quantified_params
      params.require(:quantified).permit(:categories, :metric, :date, :comment, :private_submit, :tag_list, results_attributes: [:id, :result_value, :date_value, :good, :_destroy])
    end
end

结果模型

class Result < ActiveRecord::Base
	belongs_to :user
  belongs_to :quantified
	has_many :comments, as: :commentable
  default_scope { order('date_value DESC') }
	scope :good, -> { where(good: true) }
	scope :good_count, -> { good.count }
end

感谢您的时间和专业知识。

【问题讨论】:

    标签: ruby-on-rails ruby conditional


    【解决方案1】:

    你可以这样做:

    <% averaged.results.each do |result| %>
      <div class="<%= result.good? ? 'green' : 'red' %>">
    

    在 Rails 中,ActiveRecord 将自动在每个布尔字段上定义一个方法,该方法将始终返回 truefalse(从不 nil):

    # example
    # User's boolean attribute 'is_admin' in the DB
    # Rails will define an instance method called `is_admin?`:
    User.new(is_admin: true).is_admin? # => true
    User.new(is_admin: false).is_admin? # => false
    User.new(is_admin: nil).is_admin? # => false
    # the defined method's name is `<boolean_attribute_name>?`
    

    另外你不需要像这样明确:

    if result.good == true
      #etc.
    end
    

    你可以写:

    if result.good
      #etc.
    end
    

    如果result.good 返回falsenil,则不会进入 if 块。如果它返回 falsenil 以外的任何内容,它将执行 if 块。

    【讨论】:

    • Yoshiji 先生来救援!但由于加利先生的无能,他仍然陷入困境。您的代码创建了与我之前“通过为每个提交的结果呈现额外的量化/结果”相同的错误。我不认为我写错了。并感谢==true 的解释。
    • 这是因为您将 joins 的结果添加到 Quantified,它为与之关联的每个 result 复制了 quantified 记录。请改用Quantified.includes(:results)
    • 使用您的代码或将其取出没有区别。我就不说了。但问题仍然存在:(
    • 你能把范围averagedinstance的源代码贴出来吗?此外,您的代码中存在一个缺陷:如果没有 current_user,则不会设置 3 个变量 @averaged_quantifieds 等,但在您看来,您无需测试即可直接调用它们:您最终会得到一个“未定义的方法 .each for nil:NilClass”
    • 我不明白为什么 result 会被渲染两次。您的代码似乎没问题,但您的问题是关于如何使用布尔属性来更改字体颜色。最终为其他问题的双重渲染创建一个新线程
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 2011-01-14
    • 1970-01-01
    • 2020-07-17
    相关资源
    最近更新 更多