Skip to main content
Ask A Question
Notifications
You’re not receiving notifications from this thread.
Subscribe

Use the helper methods that are declared in controller and has default params in view spec ?

Rails • Asked by Uzval

Here's the problem:
I have a Controller (say ExampleController) which has a method "sort_column" and it has the following code :
Example.column_names.include?(params[:sort]) ? params[:sort] : 'title'
which says that, In the "Example" model, if you find any column names that include the params[sort], whitelist them.
I have declared this method "sort_column" as a helper method in my controller by declaring
helper_method :sort_column

Now, my question is that, upon render of index page, i sorted the table by default to have asc order. If i try to test the index.html.erb, which renders by having the sort_column as 'title', i am blocked with an error saying this

Failure/Error: direction = (column == sort_column) && (sort_direction == 'asc' ? 'desc' : 'asc')

 ActionView::Template::Error:
   undefined local variable or method `sort_column' for #<#<Class:0x00007fd4f085c380>:0x00007fd4eb195c90>

Hitting my head from a couple of days with this error !


Controller methods are not available in the view unless you expose them with helper_method :sort_column


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 30,242+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.