Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 3 从其他控制器访问局部布局中的变量_Ruby On Rails 3_Variables_Partial Views_View Helpers - Fatal编程技术网

Ruby on rails 3 从其他控制器访问局部布局中的变量

Ruby on rails 3 从其他控制器访问局部布局中的变量,ruby-on-rails-3,variables,partial-views,view-helpers,Ruby On Rails 3,Variables,Partial Views,View Helpers,今天我想在我的shared/菜单中使用一些变量。我知道我应该在application\u controller中放置一些助手方法(或类似的方法),但它不适合我。这是我的临时解决方案(shared/_menu.html.haml): 我不想直接在视图中定义@pagecontents变量!:/我的问题是将@pagecontent变量添加到application\u controller文件的某个位置,但当我尝试时,例如: def pagecontentforpartial @pageconten

今天我想在我的
shared/菜单中使用一些变量。我知道我应该在
application\u controller
中放置一些助手方法(或类似的方法),但它不适合我。这是我的临时解决方案(
shared/_menu.html.haml
):

我不想直接在视图中定义
@pagecontents
变量!:/我的问题是将
@pagecontent
变量添加到
application\u controller
文件的某个位置,但当我尝试时,例如:

def pagecontentforpartial
  @pagecontents = Pagecontent.all
end

然后试图在我的局部视图中访问这个变量,我得到一个错误,这个方法是
nil
(但是已经有很多记录!)有人可以告诉我如何解决这个问题吗?

您可以使用
渲染
方法直接将对象传递给局部视图

这是你的
共享菜单.html.haml

- @pagecontents = Pagecontent.all
- @pagecontents.each do |pc|
  %ul
    %li
      = link_to pc.title, pc
- pagecontents.each do |pc|
%ul
  %li
    = link_to pc.title, pc
当需要显示时,可以在视图中使用此方法

render 'shared/menu', pagecontents: @pagecontents
当然,
@pagecontents
来自控制器