Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 显示站点范围内的rails部分_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 显示站点范围内的rails部分

Ruby on rails 显示站点范围内的rails部分,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个rails应用程序,有几个视图和布局。我的一个布局名为user\u admin。在这里,我有一个侧边栏,我想显示所有注册的“租户” 我有一个属性仪表板,其中包含一个租户部分和工作良好。为了让上述想法在侧栏中发挥作用,我创建了一个共享部分,然后用 <%= render :partial => "shared/sidebar_tenant_list", :locals=>{ :tenants => @property.tenants} %> 提取的源(在第13

我有一个rails应用程序,有几个视图和布局。我的一个布局名为
user\u admin
。在这里,我有一个侧边栏,我想显示所有注册的“租户”

我有一个属性仪表板,其中包含一个租户部分和工作良好。为了让上述想法在侧栏中发挥作用,我创建了一个共享部分,然后用

<%= render :partial => "shared/sidebar_tenant_list", :locals=>{ :tenants => @property.tenants} %>
提取的源(在第133行附近):

131:
132:                
133:“共享/侧栏\租户\列表”,:locals=>{:tenants=>@property.tenants}%>
134:                
135:  
有没有一种方法可以让整个站点的所有视图都可以使用

谢谢你的帮助…T

而不是

<%= render :partial => "shared/sidebar_tenant_list", :locals=>{ :tenants => @property.tenants} %> 
“共享/侧栏\u租户\u列表”,:locals=>{:tenants=>@property.tenants}%>
你可以写

<%= render :partial => "shared/sidebar_tenant_list", :locals=>{ :tenants => Tenant.all } %> 
“共享/侧栏\u租户\u列表”,:locals=>{:tenants=>tenant.all}%>

因此,所有帐篷都将显示在您的局部区域中。并且未绑定到环境变量。

您可以向ApplicationController添加:before\u筛选器,该筛选器将在每个控制器中的每个操作之前执行

class ApplicationController < ActionController::Base

  before_filter :load_property

  def load_property
    @property = ...Whatever...
  end

end
class ApplicationController
问题似乎是
@property
为零,无论您在何处使用此调用
@property.tenants的部分边栏片段,@属性都必须可用

一个可能的解决方案可能是在控制器中设置一个查找@property的before_过滤器,如下所示:

class SidebarController < ApplicationController
   before_filter :lookup_property

   def lookup_property
     @property = YourProperyModel.find(...)
   end
end


# any controller that needs a sidebar
class SomeControllerWithSidebar < SidebarController
  # some sugar if it's not possible to lookup @property for a certain action
  skip_before_filter :lookup_property, :only => [:new]

  # any action 
  def someaction
 end
class SidebarController[:new]
#任何行动
定义动作
结束

结束

其他页面上的@property听起来像是空的。您是否要在任何地方使用该变量?在
@property
对象中有什么?如果它是静态的,你可以使用一个助手,而不是一个好的解决方案-@属性可能只有某些租户,而不是所有租户。线程开启器写了显式
,我想显示所有注册的“租户”。
他已经有一个部分,可以这样做,但只能在特定的操作中工作。简单而且有效!谢谢你的工作就像一个魅力。我本应该知道的。对不起@davidb,我没有看到+1;)
class ApplicationController < ActionController::Base

  before_filter :load_property

  def load_property
    @property = ...Whatever...
  end

end
class SidebarController < ApplicationController
   before_filter :lookup_property

   def lookup_property
     @property = YourProperyModel.find(...)
   end
end


# any controller that needs a sidebar
class SomeControllerWithSidebar < SidebarController
  # some sugar if it's not possible to lookup @property for a certain action
  skip_before_filter :lookup_property, :only => [:new]

  # any action 
  def someaction
 end