我在控制器中的辅助方法

发布于 2024-08-28 03:13:31 字数 1216 浏览 5 评论 0原文

我的应用程序应该呈现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷清清 2024-09-04 03:13:31

助手只是 ruby​​ 模块,您可以像任何模块一样将其包含在任何控制器中。

module UserHelper
    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
end

并且,在您的控制器中:

class UserController < ApplicationController
    include UserHelper

    def create
        redirect_to link_to_profile(User.first)
    end
end

helpers are just ruby modules which you can include in any controller just like any module.

module UserHelper
    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
end

And, in your controller :

class UserController < ApplicationController
    include UserHelper

    def create
        redirect_to link_to_profile(User.first)
    end
end
牵你的手,一向走下去 2024-09-04 03:13:31

奥基。让我们回顾一下。您想要访问某些函数/方法,但不希望这些方法附加到当前对象。

所以你想创建一个代理对象,它将代理/委托给这些方法。

class Helper
  class << self
   #include Singleton - no need to do this, class objects are singletons
   include ApplicationHelper
   include ActionView::Helpers::TextHelper
   include ActionView::Helpers::UrlHelper
   include ApplicationHelper
  end
end

并且,在控制器中:

class UserController < ApplicationController
  def your_method
    Helper.link_to_profile
  end
end

这种方法的主要缺点是,从辅助函数中,您将无法访问控制器上下文(例如,您将无法访问参数、会话等),

折衷方案是将这些函数声明为在辅助模块中是私有的,因此,当您包含该模块时,它们在控制器类中也将是私有的。

module ApplicationHelper
  private
  def link_to_profile
  end
end

class UserController < ApplicationController
  include ApplicationHelper
end

,正如 Damien 指出的那样。

更新:您收到“url_for”错误的原因是您无权访问控制器的上下文,如上所述。您可以强制将控制器作为参数传递(Java 风格;)),例如:

Helper.link_to_profile(user, :controller => self)

然后,在您的帮助程序中:

def link_to_profile(user, options)
  options[:controller].url_for(...)
end

或者发生更大的黑客事件,呈现 此处。但是,我建议将方法设为私有并将它们包含在控制器中的解决方案。

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.

class Helper
  class << self
   #include Singleton - no need to do this, class objects are singletons
   include ApplicationHelper
   include ActionView::Helpers::TextHelper
   include ActionView::Helpers::UrlHelper
   include ApplicationHelper
  end
end

And, in controller:

class UserController < ApplicationController
  def your_method
    Helper.link_to_profile
  end
end

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.

module ApplicationHelper
  private
  def link_to_profile
  end
end

class UserController < ApplicationController
  include ApplicationHelper
end

, 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:

Helper.link_to_profile(user, :controller => self)

and then, in your helper:

def link_to_profile(user, options)
  options[:controller].url_for(...)
end

or event a bigger hack, presented here. However, i would reccomend the solution with making methods private and including them in the controller.

过期情话 2024-09-04 03:13:31

拿那个! 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文