设计强大的参数不允许:头像

问题描述 投票:0回答:1

我在application_controller as per the documentation中为设计设置了自定义参数。但是,当我使用:avatar图像提交表单时,它会在控制台中显示未允许的参数错误。

我正在使用带有carrierwave 1.0的rails 5进行上传,并设计4.4.1。据我所知这应该有效,我希望有人可以帮我弄清楚我做错了什么。

安慰:

Processing by RegistrationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"RHtEqjx50kMKQuIsHQoR3vjjzK1v3mDM3j4N8K2HSb+r3uFtlkrBmDFdpCi/SB5iB92WGB5cBB5ZlBaIYk0PTQ==", "user"=>{"username"=>"testuser3", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007fb1582c5e50 @tempfile=#<Tempfile:/tmp/RackMultipart20190228-9699-1ypcbkw.jpg>, @original_filename="1-mens-spiky-undercut-haircut.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"1-mens-spiky-undercut-haircut.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Update"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 4], ["LIMIT", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 4], ["LIMIT", 1]]
Unpermitted parameter: :avatar
   (0.1ms)  begin transaction
   (0.0ms)  commit transaction
Redirected to https://81cd3c30040546168ef9f84eb7714203.vfs.cloud9.us-east-2.amazonaws.com/
Completed 302 Found in 160ms (ActiveRecord: 0.5ms)

Application_controller:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :channel_variable
  before_action :configure_permitted_parameters, if: :devise_controller?

    def channel_variable
    @channels = Channel.all.order('created_at desc')
    end

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:avatar, :avatar_cache, :remove_avatar])
    devise_parameter_sanitizer.permit(:sign_in, keys: [:avatar, :avatar_cache, :remove_avatar])
    devise_parameter_sanitizer.permit(:account_update, keys: [:avatar, :avatar_cache, :remove_avatar])
  end

end

应用程序/视图/设计/注册/ 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.input :username, required: true,  input_html: { class: "input"}, wrapper: false, label_html: { class: "label" } %>
            </div>
          </div>

          <div class="field">
            <div class="control">
              <%= f.input :email, required: true, input_html: { class: "input"}, wrapper: false, label_html: { class: "label" } %>
            </div>
          </div>

          <div class="field">
          <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
            <p>Currently waiting confirmation for: <%= resource.unconfirmed_email %></p>
          <% end %>
          </div>

          <div class="field">
            <div class="control">
            <%= f.input :password, autocomplete: "off", hint: "leave it blank if you don't want to change it", required: false, input_html: { class: "input"}, wrapper: false, label_html: { class: "label" } %>
            </div>
          </div>

          <div class="field">
            <div class="control">
            <%= f.input :password_confirmation, required: false, input_html: { class: "input"}, wrapper: false, label_html: { class: "label" } %>
            </div>
          </div>

          <div class="field">
            <div class="control">
              <%= f.input :current_password, hint: "we need your current password to confirm your changes", required: true, input_html: { class: "input"}, wrapper: false, label_html: { class: "label" } %>
            </div>
        </div>

          <div class="field">
           <div class="control">
          <%= f.file_field :avatar %>
          </div>
          </div>

        <%= f.button :submit, "Update", class:"button is-info" %>

      <% end %>
ruby-on-rails devise carrierwave
1个回答
1
投票

用下面的代码替换你的方法configure_permitted_parameters。如果出现相同的错误,请告诉我。

protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:username, :email, :password,
      :password_confirmation, :remember_me, :avatar, :avatar_cache, :remove_avatar) }

    devise_parameter_sanitizer.permit(:sign_in, keys: [:avatar, :avatar_cache, :remove_avatar])

    devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:username, :email, :password,
      :password_confirmation, :current_password, :avatar, :avatar_cache, :remove_avatar) }
  end
© www.soinside.com 2019 - 2024. All rights reserved.