![](/img/trans.png)
[英]Unable to permit :file param in rails controller when trying to upload file using active storage
[英]How to permit a new param with devise for active storage?
我使用的是帶有Rails 5.2的最新版本的devise 4.6.1。 由於某些原因,在應用程序控制器中將參數列入白名單無效。 這段代碼直接從文檔中獲取,我不知道為什么它不起作用。
Application_controller:
class ApplicationController < ActionController::Base
# protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :channel_variable
def channel_variable
@channels = Channel.all.order('created_at desc')
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:account_update, keys: [:avatar])
devise_parameter_sanitizer.permit(:sign_in, keys: [:avatar])
devise_parameter_sanitizer.permit(:sign_up, keys: [:avatar])
end
end
安慰:
Unpermitted parameter: :avatar
edit.html.erb:
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= f.error_notification %>
<div class="field">
<div class="control">
<%= f.label :avatar %>
<%= f.file_field :avatar %>
</div>
</div>
<% end %>
我發現最好創建一個單獨的路由,並使用UsersController
修改身份驗證之外的User
屬性,包括Active Storage
附件。 您想要創建一個users#update
方法,並將您的avatar
參數列入白名單。 這有點過時,但可接受的答案基本上是您要采用的方法:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.