[RUBY] I want to be able to read a file using refile with administrate [rails6]

The hardware uses MacBook Air, and the development environment uses VScode. ruby2.6.5 rails6.0.3.2

When I used administrate for the administrator gem and refile for the image reading gem on rails6, I was addicted to the cooperation between administrate and refile, so I will introduce the details and solution.

By the way, administrate_field_refile did not support rails6, so I tried my best not to use it!

On the administrator screen (localhost: 3000 / admin), when I try to add new data, the input part of the image is not a file selection but a text box.

When I go to localhost: 3000 / admin and try to create a new User, the profile_image part is a text box. (Bottom of the second image)

スクリーンショット 2020-09-08 21.47.03.png スクリーンショット 2020-09-08 21.47.12.png

So, in order to solve this, it seems that there is a gem called administrate_field_refile when I researched various things, but since this was not compatible with rails6, I decided to proceed in the direction of customizing administrate.

Customize administrate

First of all, let's display the code inside administrate, the part that will be customized, locally. Refer to administrate documentation and add controllers, views, and fields for dashbord.

$ rails generate administrate:dashboard User
$ rails generate administrate:views User
$ rails generate administrate:field refile

Next, rewrite the profile_image_id: Field :: String, of dashbord as follows. Reference; http://administrate-prototype.herokuapp.com/adding_custom_field_types

app/dashboards/user_dashboard.rb


  ATTRIBUTE_TYPES = {
   ~abridgement~
   profile_image_id: RefileField,
  }.freeze

Then change the views of the form from the textbox to Select File.

:app/views/fields/refile_field/_form.html.erb



<div class="field-unit__label">
  <%= f.label field.attribute %>
</div>
<div class="field-unit__field">
  <%= f.attachment_field :profile_image, direct: field.direct, presigned: field.presigned, multiple: field.multiple %>
</div>

Then rewrite app / fields / refile_field.rb as follows:

app/fields/refile_field.rb


require "administrate/field/base"

class RefileField < Administrate::Field::Base
  def to_s
    data
  end

  def direct
    options.fetch(:direct, false)
  end

  def presigned
    options.fetch(:presigned, false)
  end

  def multiple
    options.fetch(:multiple, false)
  end
end

If you leave it as it is, the red text "Unpermitted parameters:" will be displayed on the console and you will not be able to set the image, so allow it below. (In ~~~, put the element to be registered. If there is any omission, it will be displayed in red in the terminal, so check and add it each time)

python


def resource_params
  params.require(:user).permit(:profile_image,:~~~,:~~~,:~~~)
end

Perhaps this will allow you to register image data from administrate.

If you want to be able to set a password or make the registration screen easier to see, please refer to the following. http://blog.319ring.net/2016/05/14/custom_view_administrate/


If you find it useful, please click the LGTM button: raised_hand_tone1:

Recommended Posts

I want to be able to read a file using refile with administrate [rails6]
I want to monitor a specific file with WatchService
I want to add a browsing function with ruby on rails
I want to play with Firestore from Rails
[Rails] I want to load CSS with webpacker
I want to download a file on the Internet using Ruby and save it locally (with caution)
Rails6 I want to make an array of values with a check box
I want to use a little icon in Rails
I want to authenticate users to Rails with Devise + OmniAuth
I want to define a function in Rails Console
I want to read the property file with a file name other than application.yml or application- [profile name] .yml with Spring Boot.
I want to find a relative path in a situation using Path
[Rails] I tried to create a mini app with FullCalendar
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I want to create a form to select the [Rails] category
[Rails] I want to display "XX minutes ago" using created_at!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to create a Parquet file even in Ruby
I tried to be able to pass multiple objects with Ractor
I want to convert an array to Active Record Relation with Rails
I want to be able to think and write regular expressions myself. ..
I want to hook log file generation / open with log4j # FileAppender
I tried to build a simple application using Dockder + Rails Scaffold
[Rails] I want to add data to Params when transitioning with link_to
I want to extract between character strings with a regular expression
I tried to make a group function (bulletin board) with Rails
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
Fixed to be able to save a specific column empty with active record
[Rails] I want to send data of different models in a form
I want to write a nice build.gradle
I want to create a Servlet war file with OpenJDK on CentOS7. Without mvn. With no internet connection.
I made an app to scribble with PencilKit on a PDF file
I want to select multiple items with a custom layout in Dialog
I want to use DBViewer with Eclipse 2018-12! !!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to write a unit test!
[Note] I want to get in reverse order using afterLast with JdbcTemplate
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I want to display a PDF in Chinese (Korean) with thin reports
If you want to make a zip file with Ruby, it's rubyzip.
I want to ForEach an array with a Lambda expression in Java
I want to introduce the committee with Rails without getting too dirty
[Rails] How to read the XML file uploaded from the screen with Hash type
[Android] I want to create a ViewPager that can be used for tutorials
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
How to create an Excel form using a template file with Spring MVC
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
[Rails] I want to test with RSpec. We support your step [Introduction procedure]
I want to test Action Cable with RSpec test
Read a string in a PDF file with Java
[Rails] rails new to create a database with PostgreSQL
[Rails] How to create a graph using lazy_high_charts
Write to a file using ShiftJIS-Read a file (Kotlin / JVM)
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
I want to simply write a repeating string
[Rails] I made a draft function using enum
I want to design a structured exception handling