rails 3.0.3 embedded views
This question is a direct result of my ignorance to rails, and therefore terrible search queries. 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.
Here's how its set up, I've got two helper methods sort_by
and sort_direction
in my inventory controller. 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 prob开发者_JAVA百科lem is that I'm using sort_by
and sort_direction
while on the user controller. 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). 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.
That said, if you aren't using those methods in controller actions, move them into a helper, as well.
精彩评论