New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with form for uploading file in the AA #3476
Comments
You need to write a wrapper or alias method like this: def uploaded_file= file
self.location = file.read
end or you can do this (not recommended): def location= file
super file.read
end Thats because ActionDispatch don't give you the file content, it gives you a |
Thanks for your immediate input, @timoschilling. Appreciate it. Just a couple of follow up questions:
|
I know what do you want to do with your You should have a controller method like this: def create
@document = Document.new params # example without permitted params
# redirect or render
end This is that what the AA #create does! And a model like this: model Document < ActiveRecord::Base
STORE_DIR = "documents"
STORE_PATH = File.join("public", STORE_DIR)
Dir.mkdir(STORE_PATH) unless Dir.exists?(STORE_PATH) # this should not be done here, move it into the server setup
def file= file
self.doc_type = file.original_filename.split('.').last
self.location = File.join(STORE_DIR, file.original_filename)
File.open(File.join(STORE_PATH, file.original_filename), "wb") { |f| f.write(file.read) }
end
end And this is what you form looks like: form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs :multipart => true do
f.input :name
f.input :file, :as => :file
f.input :static_page_id, :label => 'Parent Static Page', :as => :select, :collection => ->{StaticPage.pluck(:title, :id) }
end
f.actions
end |
Thank you for your detailed solution. You are very right about the slim controller and fat model. I will try to make those that way from now on. I've implemented your suggestion for file but there is a strange error I can't seem to understand. Tried a few different solutions and it's still there. It says:
for I've read that this may be because of missing ":multipart => true" in the form but mine is there. |
I think the |
It was on the inputs but I had Controller:
Model:
**Active Admin form:
|
You can't (re)use the location location method, for your 'upload' method. def file= file
self.doc_type = file.original_filename.split('.').last
self.location = File.join(STORE_DIR, file.original_filename)
File.open(File.join(STORE_PATH, file.original_filename), "wb") { |f| f.write(file.read) }
end |
Ok, that's totally my mistake. I though :file in the form of your example is a field that needs to persist in the database and replaced it with :location not thinking about the fact I cant use self.location = in the same function as my intention was. Now (after moving Thanks a whole lot for your valuable help. I wouldn't of been able to solve this on my own even spending a week more trying to figure it out. Best Regards, |
Ok, you are right, I don't have see the real problem ;) I'm glad to have helped you! |
Hey, sorry for continuously bothering you, but I was trying to implement your advice on moving the line for creating uploads/documents.
Thank you in advance for your precious input. |
You can place it in a capistrano setup, or if you don't have a server setup tool, than place it in a initializer |
Cool, thanks again. I guess it will be in initializer then. |
Just want to share my finding that |
Hello,
Could you please, assist me with information about an issue I have. I've decided to implement my own file upload functionality. It worked fine when (for test) implemented on the front-end. When I decided to take it back-end in AA, a strange error started popping. I have suspicion it might be of some expectation on the AA side that
f.input :location, :as => :file is expecting field that contains file in the DB and not a string field that is containing path to the file on the storage place.
More detailed explanation and example of the problem could be found here>
http://stackoverflow.com/questions/26215068/cant-cast-actiondispatchhttpuploadedfile-to-string-in-activeadmin?noredirect=1#comment41113413_26215068
Thank you in advance for your help.
Best Regards
The text was updated successfully, but these errors were encountered: