简体   繁体   English

controller 中的 Rails 6 访问辅助方法

[英]Rails 6 access helper method in controller

An application must communicate to an API a link.应用程序必须与 API 链接通信。 Thus the controller tries to define a variable as follows:因此 controller 尝试定义一个变量如下:

link = helper.link_to(I18n.t 'cart.check_cart_status', acqs_url)

However in the console this returns但是在控制台中这返回

NameError (undefined local variable or method `acqs_url' for main:Object)

when this methods respects the definition in the API docs .当此方法遵循 API 文档中的定义时

Access to the helpers in controllers has changed quite a bit from version to version.访问控制器中的助手在不同版本之间发生了很大变化。 What is wrong with the above syntax in Rails 6.1.3? Rails 6.1.3 中的上述语法有什么问题? or is the call to I18n gumming things up?还是调用 I18n 搞砸了?

You're missing parantheses around the I18n.t argument - it should be link_to(I18n.t('cart.check_cart_status'), acqs_url) , otherwise the URL will be passed as an argument to I18.t method, not to the link_to method.您缺少围绕I18n.t参数的括号 - 它应该是link_to(I18n.t('cart.check_cart_status'), acqs_url) ,否则 URL 将作为参数传递给I18.t方法,而不是link_to方法。

Also, if you're trying to test the assignment in the rails console, you should call it like this:此外,如果您尝试在 Rails 控制台中测试分配,您应该这样称呼它:

helper.link_to(I18n.t('cart.check_cart_status'), app.acqs_url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM