I have a model called employers and I'm using paperclip to upload images. It was working before but for some reason, it has stopped working. I am asking the user to fill a form out with name, company, and a profile pic. In my controller, I have set it so the form doesn't get submitted if something is wrong. The user gets redirected to the homepage if the form gets submitted and the form is rendered again if something is wrong. Right now, the form gets submitted. In my development logs, the name of the picture is also there. My development long looks like this (I am updating the form here, same concept as new form):
Started PATCH "/employers/werwer" for ::1 at 2017-06-28 21:37:15 -0400
Processing by EmployersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"JeftOiYu26+Vxo/f6NSigrUzHaTsukGvIoGxUVJkH4OGK+HcAs7fH5DWWDHnZiFTYs0/YqN4ZhAYxgwqmSfo7w==", "employer"=>{"name"=>"werwer", "company"=>"werwe", "position"=>"werwer", "number"=>"345345", "email"=>"dinukap999@gmail.com"}, "picture"=>#<ActionDispatch::Http::UploadedFile:0x0000000b570bb0 @tempfile=#<Tempfile:C:/Users/dinuka/AppData/Local/Temp/RackMultipart20170628-7872-a8xds6.png>, @original_filename="icons8-Manager-48.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"picture\"; filename=\"icons8-Manager-48.png\"\r\nContent-Type: image/png\r\n">, "commit"=>"Update Employer", "id"=>"werwer"}
[1m[35mEmployer Load (1.0ms)[0m SELECT "employers".* FROM "employers" WHERE "employers"."slug" = ? ORDER BY "employers"."id" ASC LIMIT 1 [["slug", "werwer"]]
[1m[36m (1.0ms)[0m [1mbegin transaction[0m
[1m[35m (1.0ms)[0m commit transaction
Redirected to http://localhost:3000/employers/werwer
Completed 302 Found in 35ms (ActiveRecord: 3.0ms)
As you can see, the pic with name "a8xds6.png" is shown on the the third line. But I'm not sure why it's submitted as a temp file.
When I open up the rails console and see the data of that particular employer, everything else except the image is updated. The image says "nil".
This is my form:
<%= simple_form_for @employer do |f| %>
<%= f.input :name, required: true, label: 'Full name' %>
<%= f.input :company, required: true, label: 'Name of company' %>
<%= f.file_field :emp_img, required: true, name: 'picture' %>
<%= f.button :submit %>
<% end %>
Controller:
class EmployersController < ApplicationController
before_action :find_employer, only: [:show, :edit, :update, :destroy]
def index
end
def show
end
def new
@employer = current_user.build_employer
end
def create
@employer = current_user.build_employer(employer_params)
if @employer.save
redirect_to userinfos_path
else
render 'new'
end
end
def edit
end
def update
if @employer.update(employer_params)
redirect_to employer_path(@employer)
else
render 'edit'
end
end
def destroy
@employer.destroy
redirect_to root_path
end
private
def employer_params
params.require(:employer).permit(:name, :company, :emp_img)
end
def find_employer
@employer = Employer.friendly.find(params[:id])
end
end
Employer model:
class Employer < ActiveRecord::Base
belongs_to :user
has_attached_file :emp_img, :styles => { :profile_pic => "200x200>" }, :default_url => "/images/:style/missing10.png"
validates_attachment_content_type :emp_img, :content_type => /\Aimage\/.*\Z/
end
View:
<%= image_tag @employer.emp_img.url(:profile_pic) %>
Please let me know what I need to change or if you need any more information. I know the development logs show more parameters than that are in the forms, I removed some params to simplify the question.
Your emp_img file_field should not have a custom name attribute, if you do so you should permit it and add an attr_accessor.
private
def employer_params
params.require(:employer).permit(:name, :company, :emp_img, :picture)
end
Employer model:
class Employer < ActiveRecord::Base
[...]
attr_accessor :picture
[...]
end
File uploads to server will be saved as a temp file in tmp folder and rails will handle it with ActionDispatch, after your rails object saved the temp file will be moved to the folder that you specified and then save the directory path.
You should remove the name attribute in file_field.
In your form, If you look closely. You can see, you can changed name property to picture which should be emploer[emp_img]
.
Due to this emp_imp
not present in employer
params, and not updating only emp_img
.
Try change
<%= f.file_field :emp_img, required: true, name: 'picture' %>
to
<%= f.file_field :emp_img, required: true %>
This will solve your problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.