Home > Mobile >  Rails 6 access helper method in controller
Rails 6 access helper method in controller

Time:02-26

An application must communicate to an API a link. Thus the controller tries to define a variable as follows:

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.

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 ? or is the call to I18n gumming things up?

CodePudding user response:

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.

Also, if you're trying to test the assignment in the rails console, you should call it like this:

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