[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? ?? ??

environment

Conclusion

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

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

Digression

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

helpers/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. スクリーンショット 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
[Rails] How to get the user information currently logged in with devise
How to display the text entered in text_area in Rails with line breaks
How to check Rails commands in the terminal
[Rails] How to register multiple records in the intermediate table with many-to-many association
[rails] How to use devise helper method before_action: authenticate_user!
Organized how to interact with the JDK in stages
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 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
[Rails] How to write in Japanese
How to introduce jQuery in Rails 6
How to get along with Rails
How to install Swiper in Rails
[Rails / Routing] How to refer to the controller in the directory you created
How to deal with 405 Method Not Allowed error in Tomcat + JSP
[Rails] How to introduce kaminari with Slim and change the design
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
I tried to understand how the rails method "link_to" is defined
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
[Rails] How to reset the database in production environment (Capistrano version)
How to write the view when Vue is introduced in Rails?
How to make an application with ruby on rails (assuming that the environment has been built)
How to build an environment with Docker, which is the minimum required to start a Rails application
[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
[Ruby on Rails] How to log in with only your name and password using the gem devise
How to use custom helpers in rails
[Java] How to compare with equals method
[Rails] How to log in with a name by adding a devise name column
[Rails] How to use rails console with docker
How to insert a video in Rails
[Rails / simple_format] Helper method that reflects line breaks entered in the form
[Java] How to use the toString () method
How to use MySQL in Rails tutorial
[Rails] "pry-rails" that can be used when saving with the create method
What is the main method in Java?
[rails] How to configure routing in resources
How to get the date in java
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