我们可以从视图中调用 Controller 的方法吗(理想情况下我们从 helper 调用)? [英] Can we call a Controller's method from a view (as we call from helper ideally)?

查看:26
本文介绍了我们可以从视图中调用 Controller 的方法吗(理想情况下我们从 helper 调用)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails MVC 中,您能否从视图调用控制器的方法(因为方法可以从助手调用)?如果是,如何?

In Rails MVC, can you call a controller's method from a view (as a method could be called call from a helper)? If yes, how?

推荐答案

答案如下:

class MyController < ApplicationController
  def my_method
    # Lots of stuff
  end
  helper_method :my_method
end

然后,在您看来,您可以在 ERB 中使用 <%<%= 完全按照您期望的方式引用它:

Then, in your view, you can reference it in ERB exactly how you expect with <% or <%=:

<% my_method %>

这篇关于我们可以从视图中调用 Controller 的方法吗(理想情况下我们从 helper 调用)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆