Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

Uzval asked in Rails

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 !

Reply

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

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,265+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.