Ruby on rails 应用程序控制器中的全局变量?

Ruby on rails 应用程序控制器中的全局变量?,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,Ruby On Rails 3.2,我在第一个国家与 @country = Country.find(1) 然后我在我的头部导航我做这个循环以获得正确的标签: %ul.thumbnails - @country.tags.find_each(:conditions => "active_house = true") do |a| %li.span2 .thumbnail - a.attachments.limit(1).each do |b| = imag

我在第一个国家与

 @country = Country.find(1)
然后我在我的头部导航我做这个循环以获得正确的标签:

%ul.thumbnails
 - @country.tags.find_each(:conditions => "active_house = true") do |a|
     %li.span2
       .thumbnail
         - a.attachments.limit(1).each do |b|
           = image_tag(b.file.url)
         .caption
           %p 
             #{link_to a.url, tag_country_region_houses_path(@country, a.name), :class => 'btn-nav-drop'}
这个很好用。但是导航是全局的,所以我在application_controller中创建了如下方法:

helper_method :tags

def tags
      @country = Country.find(1)
      @tags = @country.tags.find_each(:conditions => "active_house = true")
end 
在导航视图中:

%ul.thumbnails
  - tags do |a|
      %li.span2
       .thumbnail
         - a.attachments.limit(1).each do |b|
           = image_tag(b.file.url)
         .caption
           %p 
             #{link_to a.url, tag_country_houses_path(@country,  a.name), :class => 'btn-nav-drop '}
但我收到一条错误消息“未提供块(产量)”


谢谢..remco

好吧,这与全局变量无关,应该尽可能避免

你的问题就在这方面

tag_country_houses_path(@country,  a.name)
视图中不存在
@country

你可能想知道为什么。原因是helper无法将实例变量传递给View,这与controller不同

您的助手所做的就是返回一个数组对象
@tags
。此对象的值在视图中可用,但实例变量
@tags
@country
都不可用

修复?使用一些东西来替换
@country
。如果协会的国家/地区有许多标签,您可以这样做:

tag_country_houses_path(a.country,  a.name)
如果没有,您可以在标记模型中设置一个方法来获取国家/地区

您甚至可以使用一些
包含来提高查询效率,但这是另一回事了

此外,可以简化助手,而无需指定任何变量

def tags
  Country.find(1).find_each(:conditions => "active_house = true")
end 

这与全局变量无关,应该尽可能避免

你的问题就在这方面

tag_country_houses_path(@country,  a.name)
视图中不存在
@country

你可能想知道为什么。原因是helper无法将实例变量传递给View,这与controller不同

您的助手所做的就是返回一个数组对象
@tags
。此对象的值在视图中可用,但实例变量
@tags
@country
都不可用

修复?使用一些东西来替换
@country
。如果协会的国家/地区有许多标签,您可以这样做:

tag_country_houses_path(a.country,  a.name)
如果没有,您可以在标记模型中设置一个方法来获取国家/地区

您甚至可以使用一些
包含来提高查询效率,但这是另一回事了

此外,可以简化助手,而无需指定任何变量

def tags
  Country.find(1).find_each(:conditions => "active_house = true")
end 

find_每个
都接受将产生的块。因为您在helper中编写find_each而不使用块,所以它将抛出一个错误。你有两个解决方案

解决方案1:您可以使用find返回一个数组

def tags
  Country.find(1).tags.find(:all, :conditions => "active_house = true")
end
在你看来:

- tags.each do |t|
  .........
解决方案2:可以将块传递给助手

def tags
  Country.find(1).tags.find_each(:conditions => "active_house = true") do |t|
    yield t
  end
end
在你看来

- tags do |t|
  .........

如果没有太多记录,只需使用solution1即可。

find\u每个
都接受将生成的块。因为您在helper中编写find_each而不使用块,所以它将抛出一个错误。你有两个解决方案

解决方案1:您可以使用find返回一个数组

def tags
  Country.find(1).tags.find(:all, :conditions => "active_house = true")
end
在你看来:

- tags.each do |t|
  .........
解决方案2:可以将块传递给助手

def tags
  Country.find(1).tags.find_each(:conditions => "active_house = true") do |t|
    yield t
  end
end
在你看来

- tags do |t|
  .........
如果您没有太多记录,只需使用solution1即可