Rails 4中Carrierwave的未允许参数错误

时间:2014-04-03 18:52:33

标签: ruby-on-rails-4 carrierwave

好的,真的卡在这里。阅读了所有文档和SO,但无法弄清楚发生了什么。允许用户通过使用Carrierwave上传文件来更新其配置文件的简单情况。我的日志一直显示

Unpermitted parameters: avatar

错误。

我的观点:

<%= form_for current_user, html: { multipart: true } do |f| %>
 <%= f.file_field :avatar %>
 <%= f.submit "Update Profile", remote: true, class: "btn btn-default" %>
<% end %>

我的控制员:

class UsersController < ApplicationController  

  def update
    current_user.update_attributes(user_params)
    respond_to do |format|
      format.html { render 'show'}
      format.js {}
    end
  end

  def user_params
    params.fetch(:user, {}).permit(:id, :email, :username, :password, :password_hash, :avatar, projects_attributes: [:id, :user_id, :name])
  end
end

我的模特

class User < ActiveRecord::Base
    mount_uploader :avatar, AvatarUploader
end

尚未触及上传者文件

class AvatarUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

看起来非常简单,为什么我会收到此错误?

更新:服务器日志:

Started PATCH "/users/91" for 127.0.0.1 at 2014-04-03 11:38:22 -0700
Processing by UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"+cLF9iPcZSL3Lc87o7/gPZjwgc9ySr7dOKmm6Yxhbno=", "user"=>{"username"=>"s+ Index", "avatar_attributes"=>#<ActionDispatch::Http::UploadedFile:0x007f81db4060c8 @tempfile=#<Tempfile:/var/folders/xl/wpdby5jn59q7hy9w0v2nv8xc0000gn/T/RackMultipart20140403-3890-127m17g>, @original_filename="cover.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar_attributes]\"; filename=\"cover.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Update Profile", "id"=>"91"}
  [1m[35mUser Load (0.4ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 91]]
Unpermitted parameters: avatar

1 个答案:

答案 0 :(得分:0)

遇到同样的问题,

我遇到了一个问题,因为我曾经这样填写过文档:

<%= f.file_field :avatar, multiple: true %>

将其更改为:

<%= form_for @user,  multiple: true do |f| %>
控制器中的

params.require(:user).permit(:avatar,...)