简体   繁体   English

rails 3.0.3嵌入式视图

[英]rails 3.0.3 embedded views

This question is a direct result of my ignorance to rails, and therefore terrible search queries. 这个问题是我对Rails的无知直接导致的,因此搜索查询也很糟糕。 I'm trying to place a sortable table on every page of my application. 我试图在我的应用程序的每一页上放置一个可排序的表。 Basically an inventory list needs to show up everywhere. 基本上,库存清单需要显示在任何地方。 However, on the user's profile page, I get an undefined method `sort_by` error. 但是,在用户的个人资料页面上,出现未定义的方法`sort_by`错误。

Here's how its set up, I've got two helper methods sort_by and sort_direction in my inventory controller. 这是它的设置方式,我的清单控制器中有两个辅助方法sort_bysort_direction Then I've got a helper method, in application helper, that creates the link: 然后,我在应用程序帮助器中有了一个帮助器方法,该方法创建了链接:

  def sortable(column, title = nil, css_class = "sort")
    title ||= column.titleize
    css_class = column == sort_by ? css_class + " current #{sort_direction}" : css_class
    direction = column == sort_by && sort_direction == "asc" ? "desc" : "asc"
    link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
  end

I know my problem is that I'm using sort_by and sort_direction while on the user controller. 我知道我的问题是我在用户控制器上使用sort_bysort_direction But how would I access them through the inventory controller? 但是我将如何通过库存控制器访问它们?

Move those two methods to your ApplicationController , and add a helper_method :sort_by, :sort_direction line to your controller (which will expose those methods to your view as helper methods). 将这两个方法移至ApplicationController ,并向控制器添加一条helper_method :sort_by, :sort_direction行(这会将这些方法作为辅助方法显示在视图中)。 All controllers inherit from it, so any methods in there will be available to all controllers, and adding helper_method exposes them to all views. 所有控制器都继承自它,因此其中的任何方法将对所有控制器可用,添加helper_method它们公开给所有视图。

That said, if you aren't using those methods in controller actions, move them into a helper, as well. 就是说,如果您不在控制器操作中使用这些方法,则也将它们移到帮助器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM