Saturday, July 29, 2006

Calling a controller method from a view raises NoMethodError

If you put a custom function in application.rb or another controller and then try to access it from your view, you'll get the "No Method Error" similar to following:

NoMethodError in Categories#list
Showing app/views/categories/list.rhtml where line #23 raised:
undefined method `create_permalink' for #<#:0xb7931838>

The solution is to place the function as a helper function in the app/helpers/categories_helper.rb or app/helpers/application_helper.rb file.


At 11:37 AM, Blogger Sarah said...

Thanks for this info. I'm a Rails newb and couldn't figure out what I was doing wrong. :)

At 2:39 PM, Blogger David said...

But now my controller can't access that method. Is there a place to put methods that are accessible from both the controller and the views?

At 6:16 AM, Blogger spullen said...

What you would do is create a module in your lib folder. You can write your method in this file. Then make a hook method in this file like:

def self.included(base)
base.send :helper_method, :some_method

You can add more methods on by comma separating them. Then to gain access to this you just need to say include MyModule in your application.rb controller.

At 3:57 PM, Blogger JadedEvan said...

I had a method, we'll call it is_foo? that I added to my ApplicationController. When I tried to call that method in the view, it wouldn't work, much like you describe. I moved is_foo? method to my ApplicationHelper file and then added this line at the top of my ApplicationController file:

include ApplicationHelper

Now, is_foo? method is available both to my controller AND to views.

At 9:49 PM, Blogger balakrishnan said...

1. class ApplicationController
3. helper_method :preivate_method
5. #code...
7. private
8. def preivate_method #changed name, CamelCase in method names is bad coding
9. #your code block
10. end
11. end

then you can call this method from both controller and views. see this for more ref.

At 6:15 PM, Blogger Adi said...

Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa pages. Thank you So much.
Oes Tsetnoc | Semangat Mengembalikan Jati Diri Bangsa


Post a Comment

<< Home

eXTReMe Tracker