Complete the code to attach a file to a model using Active Storage.
class User < ApplicationRecord has_one_attached :[1] end
The has_one_attached method defines a one-to-one attachment named avatar for the User model.
Complete the code to permit file attachment in the controller's strong parameters.
def user_params params.require(:user).permit(:name, [1]) end
To allow file uploads, the attachment name avatar must be permitted in strong parameters.
Fix the error in the view to display the attached image if present.
<% if @user.[1].attached? %> <%= image_tag @user.avatar %> <% end %>
attached? on something other than the attachment.The method attached? should be called on the attachment name avatar to check if a file is attached.
Fill both blanks to create a form field for file upload using Rails form helpers.
<%= form_with model: @user do |form| %> <%= form.[1] :[2] %> <%= form.submit %> <% end %>
text_field or image_field which do not handle file uploads.Use file_field helper with the attachment name avatar to allow file uploads in the form.
Fill all three blanks to create a hash of filenames and byte sizes for all attached files.
files_info = @document.files.map { |file| { [1]: file.filename.[2], [3]: file.byte_size } }Use filename.to_s to get the file name as a string, and byte_size for the file size. The hash keys are filename and size.
