Rails - 如何在控制器内使用帮助程序

AnA*_*ice 198 ruby-on-rails ruby-on-rails-3 ruby-on-rails-5

当我意识到你应该在视图中使用帮助器时,我需要一个帮助器在我的控制器中,因为我正在构建一个JSON对象来返回.

它有点像这样:

def xxxxx

   @comments = Array.new

   @c_comments.each do |comment|
   @comments << {
     :id => comment.id,
     :content => html_format(comment.content)
   }
   end

   render :json => @comments
end
Run Code Online (Sandbox Code Playgroud)

我怎样才能访问我的html_format助手?

gro*_*ser 286

您可以使用

  • @模板.(铁轨2)
  • view_context.(rails 3)(警告:这会实例化每次调用的新视图实例)
  • ActionController的:: Base.helpers
  • 在singleton类中包含helper,然后在singleton.helper中包含helper
  • 在控制器中包含帮助器(警告:将所有辅助方法都包含在控制器操作中)

  • 这个答案更好!在rails 3中,简单地调用`view_context.helper_function`很简单,效果很好.`ActionController :: Base.helpers.helper_function`同样出色. (55认同)
  • rails 5:在控制器中使用`helpers.helper_function`(https://github.com/rails/rails/pull/24866) (35认同)
  • `view_context` =天才 (29认同)
  • `ActionController :: Base.helpers.helper_function`似乎不起作用.我在#ControlView :: Base:0x007fede56102d0>中得到`NoMethodError - undefined method spell_date_and_time':`当试图在控制器方法中调用`ActionController :: Base.helpers.spell_date_and_time()`时.但是调用`view_context.spell_date_and_time()`确实有效. (7认同)
  • 感谢您向我介绍view_context!正是这里需要的,羞辱你的不是公认的答案! (3认同)
  • @grosser有关详细阅读此内容的任何来源?谢谢 (2认同)
  • 警告:不要使用`view_context`.它将为每个调用实例化一个新的视图实例... (2认同)

Xav*_*olt 202

注意:这是在Rails 2天内写回来的; 如今格罗瑟的回答(如下)是要走的路.

选项1:可能最简单的方法是在控制器中包含辅助模块:

class MyController < ApplicationController
  include MyHelper

  def xxxx
    @comments = []
    Comment.find_each do |comment|
      @comments << {:id => comment.id, :html => html_format(comment.content)}
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

选项2:或者您可以将辅助方法声明为类函数,并像这样使用它:

MyHelper.html_format(comment.content)
Run Code Online (Sandbox Code Playgroud)

如果您希望能够将它用作实例函数和类函数,则可以在助手中声明这两个版本:

module MyHelper
  def self.html_format(str)
    process(str)
  end

  def html_format(str)
    MyHelper.html_format(str)
  end
end
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 当您要使用的辅助方法使用视图方法(如`link_to`)时,这不起作用.控制器无法访问这些方法,我的大多数帮助程序都使用这些方法.此外,将帮助程序包含在控制器中会将所有帮助程序的方法公开为可公开访问的操作,这些操作并不好.`view_context`是Rails 3中的方法. (4认同)

Ger*_*haw 78

在Rails 5中使用helpers.helper_function您的控制器.

例:

def update
  # ...
  redirect_to root_url, notice: "Updated #{helpers.pluralize(count, 'record')}"
end
Run Code Online (Sandbox Code Playgroud)

资料来源:@Markus对另一个答案的评论.我觉得他的答案应该得到答案,因为这是最干净,最简单的解决方案.

参考:https://github.com/rails/rails/pull/24866

  • 感觉有点奇怪,在控制台中你会单调调用`helper`而在控制器中它是复数. (6认同)

小智 10

使用选项1解决了我的问题.可能最简单的方法是在控制器中包含辅助模块:

class ApplicationController < ActionController::Base
  include ApplicationHelper

...
Run Code Online (Sandbox Code Playgroud)


Fra*_*nco 9

通常,如果要在(仅)控制器中使用帮助器,我更喜欢将其声明为实例方法class ApplicationController.

  • 作为受保护的方法 (5认同)

ald*_*n.h 8

在 Rails 5 之前,您必须包含辅助模块。

在较新的版本中,您可以将控制器中的助手与助手(复数)对象一起使用。

  class UsersController
    def index
      helpers.my_helper_method_name(even_pass_arg_here)
    end
  end
Run Code Online (Sandbox Code Playgroud)

https://www.rubyguides.com/2020/01/rails-helpers/