[Rails] How to operate the helper method used in the main application with Administrate

Trouble, what you want to do

** \\ 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 ...

スクリーンショット 2020-07-21 21.00.03.png

ʻImage_present?Is ** the helper method you are using in the main app view (which you have defined inhelpers / 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


module Hoge
  class Application < Rails::Application
    #Other configs may be written

    #from here
    config.to_prepare do
      Administrate::ApplicationController.helper Hoge::Application.helpers
    #So far

Administrate::ApplicationController.helper Hoge::Application.helpers

Replace the Hoge part of with your own app name.

It was firmly listed on GitHub Issues and was resolved

Best way to include main app helper modules in administrate? #334 スクリーンショット 2020-07-21 20.51.34.png


Looking at GitHub Issues listed in the reference article, there seems to be other ways.

スクリーンショット 2020-07-21 20.52.13.png

This method is

  1. Create a ʻadministrate directory under the helpers` directory
  2. Create a ʻapplication_helper.rb` file there
  3. Redefine the helper method there It seems to be the method.

I tried it


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'
      image_tag asset_path('no-image.png'), class: 'top-cover-img shop-img'

The point is class inheritance when declaring module? It seems to write (ʻAdministrate :: Application Helper`). Without this, you will get a Routing Error. スクリーンショット 2020-07-21 21.10.13.png

Can be achieved, but violates the DRY principle

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

[Rails] How to operate the helper method used in the main application with Administrate
[Rails] How to apply the CSS used in the main app with Administrate
[Rails] How to use the map method
[Rails] How to use helper method, confimartion
How to decorate the radio button of rails6 form_with (helper) with CSS
[Rails] How to get the user information currently logged in with devise
How to check Rails commands in the terminal
[Rails] How to register multiple records in the intermediate table with many-to-many association
How to set the display time to Japan time in Rails
Organized how to interact with the JDK in stages
[How to insert a video in haml with Rails]
How to query Array in jsonb with Rails + postgres
[Rails] How to display an image in the view
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
How to get the class name / method name running in Java
How to use the getter / setter method (in object orientation)
[With back tricks] How to introduce React to the simplest Rails
[Rails] How to display information stored in the database in view
How to use the link_to method
How to use the include? method
How to use the form_with method
How to install Swiper in Rails
[Rails / Routing] How to refer to the controller in the directory you created
[Rails] How to introduce kaminari with Slim and change the design
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
I tried to understand how the rails method "redirect_to" is defined
How to install the language used in Ubuntu and how to build the environment
How to get boolean value with jQuery in rails simple form
How to rename a model with foreign key constraints in Rails
How to store the information entered in textarea in a variable in the method
How to make an application with ruby on rails (assuming that the environment has been built)
[Java] How to search for a value in an array (or list) with the contains method
Method definition location Summary of how to check When defined in the project and Rails / Gem
How to implement search functionality in Rails
How to change app name in rails
[Java] How to compare with equals method
How to insert a video in Rails
[Rails / simple_format] Helper method that reflects line breaks entered in the form
Gorigori beginners summarized how to operate the database using rails console Ntiunus
[Java] How to use the toString () method
How to have params in link_to method
How to use MySQL in Rails tutorial
[Rails] "pry-rails" that can be used when saving with the create method
[rails] How to configure routing in resources
How to compare only the time with Rails (from what time to what time, something like)
How to implement ranking functionality in Rails
How to debug the processing in the Ruby on Rails model only on the console
How to use credentials.yml.enc introduced in Rails 5.2
How to make the schema of the URL generated by Rails URL helper https
How to reference a column when overriding the column name method in ActiveRecord
[Rails] How to solve the time lag of created_at after save method
How to build Rails 6 environment with Docker
[Rails] How to read the XML file uploaded from the screen with Hash type
How to get the ID of a user authenticated with Firebase in Swift
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
How to resolve errors that occur in the "Ruby on Rails" integration test
How to output jar with main class specified by gradle in Intellij IDEA
How to make a unique combination of data in the rails intermediate table
How to set environment variables in the properties file of Spring boot application
About the case where "Docker" freeter tried to put Docker in the existing Rails application