我在控制器中的辅助方法
我的应用程序应该呈现 html,以便在用户单击 ajax 链接时进行响应。
我的控制器:
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
My app should render html, to answer when a user clicks ajax-link.
My controller:
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
My helper:
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
I have tried such methods:
ApplicationController.helpers.link_to_profile(@user)
# It raises: NoMethodError (undefined method `url_for' for nil:NilClass)
and:
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)
In addition, yes, I KNOW about :helper_method, and it works, but i don't want to overload my ApplicationController with a plenty of that methods
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
助手只是 ruby 模块,您可以像任何模块一样将其包含在任何控制器中。
并且,在您的控制器中:
helpers are just ruby modules which you can include in any controller just like any module.
And, in your controller :
奥基。让我们回顾一下。您想要访问某些函数/方法,但不希望这些方法附加到当前对象。
所以你想创建一个代理对象,它将代理/委托给这些方法。
并且,在控制器中:
这种方法的主要缺点是,从辅助函数中,您将无法访问控制器上下文(例如,您将无法访问参数、会话等),
折衷方案是将这些函数声明为在辅助模块中是私有的,因此,当您包含该模块时,它们在控制器类中也将是私有的。
,正如 Damien 指出的那样。
更新:您收到“url_for”错误的原因是您无权访问控制器的上下文,如上所述。您可以强制将控制器作为参数传递(Java 风格;)),例如:
然后,在您的帮助程序中:
或者发生更大的黑客事件,呈现 此处。但是,我建议将方法设为私有并将它们包含在控制器中的解决方案。
Oki. Let's recap. You want access to certaint functions/methods, but you don't want those methods to be attached to current object.
So you want to make a proxy object, that will proxy/delegate to those methods.
And, in controller:
The main disadvantage to this approach is that from the helper functions you won't have access to controller context (EG you won't have access to params, session, etc)
A compromise would be to declare those functions as private in the helper module, therefore, when you will include the module, they will also be private in the controller class.
, as Damien pointed out.
Update: The reason why you get the 'url_for' error is that you do not have access to controller's context, as stated above. You could force passing the controller as a parameter(Java-style ;) ) like:
and then, in your helper:
or event a bigger hack, presented here. However, i would reccomend the solution with making methods private and including them in the controller.
拿那个! http://apotomo.de/2010 /04/activehelper-rails-is-no-pain-in-the-ass/
这正是您要找的,伙计。
Take that! http://apotomo.de/2010/04/activehelper-rails-is-no-pain-in-the-ass/
That's exactly what you were looking for, dude.