Rails在设计中添加额外字段

时间:2016-02-12 07:06:24

标签: ruby-on-rails devise

我正在使用设计进行身份验证。注册页面设计正在提供3个字段,我正在尝试添加一个额外字段 file_field ..但是它给了我错误。我正在使用运营商用于文件上传的wave。我的用户表中还有一个附件列,但文件没有插入。我在控制台上收到错误。 未经许可的参数:附件。这是我的代码,任何人都可以帮我解决这个问题


[控制台]

Started POST "/users" for 127.0.0.1 at 2016-02-12 12:15:32 +0530
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"M6F9ME0zJkAxyHPETI8qmCf4a8u8bn4wTmZI0v5xtPQ=", "user"=>{"email"=>"aniketshivam@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "attachment"=>#<ActionDispatch::Http::UploadedFile:0x000000040ed928 @tempfile=#<Tempfile:/tmp/RackMultipart20160212-13081-16e7tyg>, @original_filename="73273_773684942011_4_40639956_2125564_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[attachment]\"; filename=\"73273_773684942011_4_40639956_2125564_n.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Sign up"}
Unpermitted parameters: attachment
   (0.1ms)  BEGIN
  User Exists (0.6ms)  SELECT  1 AS one FROM `users`  WHERE `users`.`email` = BINARY 'aniketshivam@gmail.com' LIMIT 1
  SQL (0.3ms)  INSERT INTO `users` (`created_at`, `email`, `encrypted_password`, `updated_at`) VALUES ('2016-02-12 06:45:32', 'aniketshivam@gmail.com', '$2a$10$kFDcpyXr1l9Mv7epJY3UMOrq9zyOInuQFQMVcqz34nHxvB.rNGk0C', '2016-02-12 06:45:32')
   (34.2ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.3ms)  UPDATE `users` SET `current_sign_in_at` = '2016-02-12 06:45:32', `current_sign_in_ip` = '127.0.0.1', `last_sign_in_at` = '2016-02-12 06:45:32', `last_sign_in_ip` = '127.0.0.1', `sign_in_count` = 1, `updated_at` = '2016-02-12 06:45:32' WHERE `users`.`id` = 6
   (40.8ms)  COMMIT
Redirected to http://localhost:3000/posts
Completed 302 Found in 183ms (ActiveRecord: 76.3ms)


Started GET "/posts" for 127.0.0.1 at 2016-02-12 12:15:32 +0530
Processing by PostsController#index as HTML
  User Load (0.4ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 6  ORDER BY `users`.`id` ASC LIMIT 1
  User Load (0.7ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 6 LIMIT 1
  Post Load (0.7ms)  SELECT  `posts`.* FROM `posts`  WHERE `posts`.`user_id` = 6  ORDER BY created_at DESC LIMIT 5 OFFSET 0
  Rendered posts/index.html.erb within layouts/application (2.9ms)
   <br>

[registration_controller]

class Users::RegistrationsController < Devise::RegistrationsController
# before_filter :configure_sign_up_params, only: [:create]
# before_filter :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  # def create
  #   super
  # end

  # GET /resource/edit
  # def edit
  #   super
  # end

  # PUT /resource
  # def update
  #   super
  # end

  # DELETE /resource
  # def destroy
  #   super
  # end

  # GET /resource/cancel
  # Forces the session data which is usually expired after sign
  # in to be expired now. This is useful if the user wants to
  # cancel oauth signing in/up in the middle of the process,
  # removing all OAuth session data.
  # def cancel
  #   super
  # end

  # protected

  # If you have extra params to permit, append them to the sanitizer.
  def configure_sign_up_params
    devise_parameter_sanitizer.for(:sign_up).permit(:attachment)
  end

  # If you have extra params to permit, append them to the sanitizer.
  # def configure_account_update_params
  #   devise_parameter_sanitizer.for(:account_update) << :attribute
  # end

  # The path used after sign up.
  # def after_sign_up_path_for(resource)
  #   super(resource)
  # end

  # The path used after sign up for inactive accounts.
  # def after_inactive_sign_up_path_for(resource)
  #   super(resource)
  # end
end


[user.rb]

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :posts
  has_many :comments

  mount_uploader :attachment, AttachmentUploader      

  def self.find_role(id)
    User.find(id).roles
  end   

end


[注册/ new.html.erb]

<h2>Sign up</h2>

<%= form_for(resource, as: resource_name,html: { multipart: true } ,url: registration_path(resource_name)) do |f| %>

  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email, autofocus: true %></div>

  <div><%= f.label :password %> <% if @validatable %><i>(<%= @minimum_password_length %> characters minimum)</i><% end %><br />
    <%= f.password_field :password, autocomplete: "off" %></div>


  <div><%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %></div>
    <br/>

    <div >
              <%= f.file_field :attachment %>

      </div>


  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>


[attachment_uploader.rb]

# encoding: utf-8

class AttachmentUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploadsss/post/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :resize_to_fit => [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  # def extension_white_list
  #   %w(jpg jpeg gif png)
  # end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

1 个答案:

答案 0 :(得分:0)

registration_controller

中尝试关注

before_filter :configure_sign_up_params

def configure_sign_up_params devise_parameter_sanitizer.for(:sign_up).push(:attachment) end