我的應用程序應該呈現html,以在用戶單擊ajax-link時回答。我在控制器中的幫助器方法
我的控制器:
def create_user
@user = User.new(params)
if @user.save
status = 'success'
link = link_to_profile(@user) #it's my custom helper in Application_Helper.rb
else
status = 'error'
link = nil
end
render :json => {:status => status, :link => link}
end
我的助手:
def link_to_profile(user)
link = link_to(user.login, {:controller => "users", :action => "profile", :id => user.login}, :class => "profile-link")
return(image_tag("/images/users/profile.png") + " " + link)
end
我曾嘗試這樣的方法:
ApplicationController.helpers.link_to_profile(@user)
# It raises: NoMethodError (undefined method `url_for' for nil:NilClass)
和:
class Helper
include Singleton
include ActionView::Helpers::TextHelper
include ActionView::Helpers::UrlHelper
include ApplicationHelper
end
def help
Helper.instance
end
help.link_to_profile(@user)
# It also raises: NoMethodError (undefined method `url_for' for nil:NilClass)
此外,是的,我知道:helper_method,它的工作原理,但我不想超載我的ApplicationController與大量的方法
是的,我已經嘗試了你的第一個方法,但我得到了:NoMethodError(未定義的方法'url_for'爲零:NilClass)。我認爲,幫助程序模塊無法訪問ActionView的幫助程序 – FancyDancy 2010-04-07 20:12:27
Yap,您必須有權訪問控制器的上下文。 – 2010-04-07 20:37:08
我更新了我的回覆,以考慮到您所說的內容。 – 2010-04-07 20:52:51