我正在将我的Rails应用程序从3.2升级到4,我运行良好,除了新用户注册Devise。我正在升级到Devise 3.2.2。当我尝试注册一个新用户时,我收到错误,“没有文件选择不能为空”用于用户的图像。我不认为这是Paperclip的问题,因为与我的其他模型一起上传的图像工作正常。我看到它说有不允许的参数,这可能是一个问题的迹象。以下是用户模型的部分:
has_attached_file :image, styles: { small: "75x108>"}
validates_attachment :image, presence: true,
content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/gif', 'image/png'] },
size: { less_than: 5.megabytes }
这是我的注册页面:
<div class="panel panel-default form_page">
<div class="panel-heading">
<h2>Sign up</h2>
</div>
<div class="panel-body input_container">
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: { class: 'form-horizontal'}) do |f| %>
<%= f.error_notification %>
<div class="form-group">
<%= f.input :image, label: "Upload A Profile Pic" %>
</div>
<div class="form-group">
<%= f.input :name, input_html: {:class => "form-control" } %>
</div>
<div class="form-group">
<%= f.input :email, input_html: {:class => "form-control" } %>
</div>
<div class="form-group">
<%= f.input :password, input_html: {:class => "form-control" } %>
</div>
<div class="form-group">
<%= f.input :password_confirmation, input_html: {:class => "form-control" } %>
</div>
<div class="form-group">
<%= f.input :twitter, label: "Enter your Twitter handle", input_html: {:class => "form-control" } %>
</div>
<div class="form-group">
<%= f.input :lastfm, label: "Enter your last.fm handle", input_html: {:class => "form-control" } %>
</div>
<div class="form-group">
<%= f.input :status, label: "Leave some words of wisdom", input_html: {:class => "form-control" } %>
</div>
<%= f.submit "Sign up", class: "btn btn-primary" %>
<% end %>
</div>
<div class="panel-footer">
<%= render "devise/shared/links" %>
</div>
这是我的服务器输出:
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"akj/sZRPil4h3tu9qntjmbgucai2h80/b8U3zhYtEoQ=", "user"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00000106bd3dd0 @tempfile=#<Tempfile:/var/folders/r8/z7hzv9r51nv9vymbj1szxqdw0000gp/T/RackMultipart20140129-36058-dwch5z>, @original_filename="tony.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[image]\"; filename=\"tony.jpg\"\r\nContent-Type: image/jpeg\r\n">, "name"=>"Tony", "email"=>"user6@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "twitter"=>"@Tambe257", "lastfm"=>"Tambe257", "status"=>"words"}, "commit"=>"Sign up"}
Unpermitted parameters: image, name, twitter, lastfm, status
(0.1ms) begin transaction
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'user6@example.com' LIMIT 1
(0.1ms) rollback transaction
答案 0 :(得分:0)
你对强参数的预感是正确的。有关强参数和设计的详细信息,请阅读this,它可以帮助您解决问题。像这样的东西,其中:username是你想要添加的字段。
class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
end
end