English 中文(简体)
Yield和缺席案件 Kluwer do 产出违约
原标题:Yield and default case || do not output default case

我有一个简单的产量使用案例,出于某种不为人知的原因,缺席案件从未显示:

在我的超级行政结构中,我有:

<%= yield :body_id ||  super_admin_main  %>

<><>My controller

class Superadmin::GolfsController < ApplicationController
  layout "super_admin"

  def show 

  end
end

www.un.org/Depts/DGACM/index_spanish.htm 我的 view见。

<% content_for(:body_id) do %>sadmin_golfs<% end %>

<>With:sadmin_golfs。

<>没有><>/>>: > 示例,而不是超级行政-main

任何人能否照搬同一行为?

最佳回答

使用母体:

<%= (yield :body_id) ||  super_admin_main  %>

<%= yield(:body_id) ||  super_admin_main  %>

如果没有的话,则假定<代码>yield(:本人_id hexachloro_admin_main)。

EDIT: Railways 3 use ActiveSupport:SafeBuffer und string/nil (Rails 2), so the production is not nil Even if there is no content_for provided. 审判:

<%= yield(:body_id).empty? ?  super_admin_main  : yield(:body_id)%>
问题回答

Try <%=产量(标题)。 ∗∗∗∗∗

。 标 :Nil(AS 3 rc docs),基本上允许具有头衔的传统合成物。

为什么在汇编中不测试内容。

在内容中,我们可以看到:

def content_for(name, content = nil, &block)
  ivar = "@content_for_#{name}"
  content = capture(&block) if block_given?
  instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{content}".html_safe)
  nil
end

因此,就您的情况而言,@content_for_本人_id? 您认为。

你可以:

<%=  instance_variable_defined?( @content_for_body_id ) ? yield(:body_id) :  super_admin_main  %>

如果你喜欢的话,你可以产生助手。

def yield_or(part, result)
  instance_variable_defined?("@content_for_#{part}") ? instance_variable_get("@content_for_#{part}") : result
end

您认为,

<%= yield_or(:body_id,  super_admin_main ) %>

它只与铁路2.3.x合作。

In Railways 3 :

http://api.rails.info/classes/ActionView/Helpers/CaptureHelper.html#M002277“rel=“nofollow noreferer”>content_for?

www.un.org/Depts/DGACM/index_spanish.htm 铁路 3

2. 提出未界定的方法

我知道这是一个老问题,但我有2.3个铁路的解决办法。

页: 1 产量: 上文提到的帮助办法,现在可以接受一个部分:

module ApplicationHelper
  def yield_或(name, content = nil, &block)
    ivar = "@content_f或_#{name}"

    if instance_variable_defined?(ivar)
      content = instance_variable_get(ivar)
    else
      content = block_given? ? capture(&block) : content
    end

    block_given? ? concat(content) : content
  end
end

可在您的模板中使用:

<% yield_或 :something do %>
    <p>something else</p>
<% end %>

<%= yield_或 :something,  something else  %>
<div class= <%= (yield :content_with_bunners).present? ? yield(:content_with_bunners) : "col-md-10"%>>

您可使用<代码>content_for? (: 本人_id),该代码将与否。

<%= content_for?(:body_id) ? yield(:body_id) :  super_admin_main  %>




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签