** \\ The helper method of the main application cannot be used /// **
This is when I used the gem ** Administrate to create an administration screen in the Rails app **. When I try to use the helper method defined in the helper file for use in the view of the main app in the view under admin, the following error occurs ...
ʻImage_present?Is ** the helper method you are using in the main app view (which you have defined in
helpers / shops_helper.rb`) **.
I don't get NoMethodError in the main app ... what should I do? ?? ??
** 1. Describe the following settings in the config / application.rb
file. ** **
** 2. Restart the application (server) **
that's all. It was easy lol
config/application.rb
module Hoge
class Application < Rails::Application
#Other configs may be written
#from here
config.to_prepare do
Administrate::ApplicationController.helper Hoge::Application.helpers
end
#So far
end
end
Hoge
is the app name. Normally, the app name when rails new was done should be listed after the module
on the first line.
Administrate::ApplicationController.helper Hoge::Application.helpers
Replace the Hoge
part of with your own app name.
Best way to include main app helper modules in administrate? #334
Looking at GitHub Issues listed in the reference article, there seems to be other ways.
This method is
directory under the
helpers` directoryhelpers/administrate/application_helper.rb
module Administrate::ApplicationHelper
def image_present?(shop)
if shop.shop_images.present?
image_tag "#{shop.shop_images[0].shop_image}", class: 'top-cover-img shop-img'
else
image_tag asset_path('no-image.png'), class: 'top-cover-img shop-img'
end
end
end
The point is class inheritance when declaring module
? It seems to write (ʻAdministrate :: Application Helper`).
Without this, you will get a Routing Error.
I could have realized it by the above method, but I thought that it was ** contrary to the DRY principle ** because the same helper method would be defined twice.
We recommend the first method because you don't have to create useless files.
Recommended Posts