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!
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)
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.
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