简体   繁体   English

Rails回形针mysql不保存文件

[英]rails paperclip mysql does not save files

The attachment resume seems to works fine, but i do not have any insert into database (null, null, null , null) and i do not have file uploaded to any folder in my app... 附件简历似乎工作正常,但我没有向数据库中插入任何内容(null,null,null,null),并且我没有文件上传到应用程序中的任何文件夹...

 class AddAttachmentCvToUsers < ActiveRecord::Migration
  def change
    change_table :users do |t|
      t.attachment :cv
    end
  end
end

User model : 用户模型:

attr_accessor :cv
validates_attachment :cv
has_attached_file :cv, :path=>":rails_root/storage/#{Rails.env}#{ENV['RAILS_TEST_NUMBER']}/."

I have also installed carrierwave but still no effects.. Edit: User Controller (I do not have another controller for attachments) : 我还安装了载波,但仍然没有效果。编辑:用户控制器(我没有用于附件的其他控制器):

class UsersController < ApplicationController

    def new
        @users = User.new
    end

    def create
        @user = User.new(user_params)
        if @user.save 
            session[:user_id] = @user.id 
            current_user = @user.id

            redirect_to '/status' 
        else 
            redirect_to '/signup' 
        end 
    end

    private
    def user_params
        params.require(:user).permit(:first_name, :last_name, :email, :PESEL, :phone, :password,:cv)
    end
end

View for new user with attachement: 查看具有附件的新用户:

<div class="login">
  <div class="container">
    <div class="form">

    <h1>SIGN UP</h1>

    <%= form_for @users,:html=> {:multipart=>true}   do |f| %>
      <%= f.text_field :first_name, :placeholder => "First name" %>
      <%= f.text_field :last_name, :placeholder => "Last name" %>
      <%= f.email_field :email, :placeholder => "Email" %>
      <%= f.text_field :PESEL, :placeholder => "PESEL number" %>
      <%= f.phone_field :phone, :placeholder => "Phone Number" %>
      <%= f.password_field :password, :placeholder => "Password" %>
      <p>CV</p>
      <%= f.file_field :cv, name: "CV", class: 'form-control' %>
      <%= f.submit "Sign up", class: "btn-submit" %>
    <% end %>


    </div>
  </div>
</div>

Some logs from console: 来自控制台的一些日志:

Started POST "/users" for 127.0.0.1 at 2016-05-18 03:55:17 +0200
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Fw6easThY51CDppDDVfqO0ProQITaltqP3DaIL3An67ey4vGXh2yEerhhhxoo3bTp/mKbkIaAmktzBviBIjg8g==", "user"=>{"first_name"=>"Test", "last_name"=>"Test", "email"=>"test@t.pl", "PESEL"=>"91020300441", "phone"=>"609123123", "password"=>"[FILTERED]"}, "CV"=>#<ActionDispatch::Http::UploadedFile:0x007f25790c1b60 @tempfile=#<Tempfile:/tmp/RackMultipart20160518-26849-rqaabr.pdf>, @original_filename="pdf-test.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"CV\"; filename=\"pdf-test.pdf\"\r\nContent-Type: application/pdf\r\n">, "commit"=>"Sign up"}
   (0.1ms)  BEGIN
  User Exists (1.4ms)  SELECT  1 AS one FROM `users` WHERE `users`.`email` = BINARY 'test@t.pl' LIMIT 1
  User Exists (0.1ms)  SELECT  1 AS one FROM `users` WHERE `users`.`PESEL` = BINARY '91020300441' LIMIT 1
  SQL (0.1ms)  INSERT INTO `users` (`first_name`, `last_name`, `email`, `PESEL`, `phone`, `password_digest`, `created_at`, `updated_at`) VALUES ('Test', 'Test', 'test@t.pl', '91020300441', '609123123', '$2a$10$HmHAiJkHv1Tada/OpzKXKOISiwumoFKTy48tFpNBYuATq/A5GaC9G', '2016-05-18 01:55:17', '2016-05-18 01:55:17')
  SQL (1.0ms)  INSERT INTO `job_apps` (`user_id`, `created_at`, `updated_at`) VALUES (35, '2016-05-18 01:55:17', '2016-05-18 01:55:17')
   (3.0ms)  COMMIT
Redirected to http://localhost:3000/status
Completed 302 Found in 135ms (ActiveRecord: 8.2ms)


Started GET "/status" for 127.0.0.1 at 2016-05-18 03:55:17 +0200
Processing by JobAppsController#index as HTML

You have overridden the name attribute of the file_field , which has broken Rails naming conventions. 您已经覆盖了file_fieldname属性,该属性破坏了Rails的命名约定。 If you remove the name attribute, Rails will be able to pass the file details uploaded in the :cv field to the database. 如果删除name属性,Rails将能够将在:cv字段中上传的文件详细信息传递给数据库。

Change the file_field to this: file_field更改为此:

<%= f.file_field :cv, class: 'form-control' %>

If you have Javascript attached to the form, and need to refer to the file_field element, you can use this, instead: 如果您在表单上附加了Javascript,并且需要引用file_field元素,则可以使用它来代替:

<%= f.file_field :cv, id: "cv", class: "form-control" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM