English 中文(简体)
如何在铁路辅助人员中用争论方式绕过一个已分结的区块呢?
原标题:How to iterate over a yielded block in a Rails helper with arguments

我想将某种逻辑抽象到一个观察助手身上。 在我看来,这个想法是能够做这样的事情:

<ul class="nav">
  <% pages_for_section( over ) do |page| %>
    <li><%= page.menu_text %></li>
  <% end %>
</ul>

我目前的做法就是让这个帮手变成这样:

def pages_for_section(section_slug, &block)
  out = []

  pages_in_section = @pages.select { |p| p.section.slug == section_slug }
  pages_in_section.each do |page|
    out << yield(page)
  end

  return out
end

具体来说, out<<< page( page) 部分正在困扰我。 它有效, 但似乎不是正确的 DRY 方式。 因此, 如果我想在视图中添加一个变量, 我将需要将它添加到块辅助器中, 并生成调用 。

底线 : 我想将一个从滚动页面到滚动的变量输入到被选中的区块。 这是最佳做法吗? 还是有更好、更可读的替代品?

理想的情况是,我想做一些事情,比如:

<ul class="nav">
  <% each_page_in_section( over ) do %>
    <li><%= page.menu_text %> <%= another_variable %></li>
  <% end %>
</ul>

和帮手 看着类似的东西 概念上是这样的:

def pages_for_section(section_slug, &block)
  pages_in_section = @pages.select { |p| p.section.slug == section_slug }
  pages_in_section.each do |page|
    another_variable = "I m cool"
    # the `page` and the `another_variable` variable will automatically 
    # be available/copied to the block
    yield
  end
end

谢谢

问题回答

如果你注射,那么你就会在注射区块内有收益率。

混乱的起因是试图让位于一个块, 区块中的代码可以让位到一个块, 然后返回一个值 。 i d 一次只做一个或另一个 。 如果您在区块内用代码让位, 不要担心返回值 。





相关问题
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: ...

热门标签