When I try to upload image using Paperclip gem I got this error:
NoMethodError (undefined method `stringify_keys' for <ActionDispatch::Http::UploadedFile:0x000000025387f0>)
class MenuItem < ActiveRecord::Base
has_one :image
end
class Image < ActiveRecord::Base
belongs_to :menu_item
has_attached_file :image, :styles => {
:large => "640x480",
:medium => "300x300",
:thumb => "100x100"
}
end
I've seen this error happen before, usually when people attempt to call update_attributes like this:
update_attributes(params[:image])
The call should actually be this:
update_attributes(:image => params[:image])
A bit of a shot in the dark, but if that's it I'm sure we'll all be impressed.
After struggling for a while in rails 3.2.2 I managed to solve this in this manner
(image = Image.new(image: params[:image])).save
update_attributes(image: image)
I just had this problem, and to clarify things a bit, update_attributes is different from update_attribute.
The following should work:
update_attributes(:image => params[:image])
or
update_attribute(:image, params[:image])
There you go! There are other issues with update_attributes related to attr_accesible, but either works.
Related
I am currently using delayed and it is up and running successfully. However, I can't seem to add a hook for when the jobs are completed from resque. The code I have currently is listening to the correct queue, but still seems to not work. I might be making a very simple, stupid mistake, but I can't seem to figure it out.
Here is the model (working fine as far as I can see):
class Entry < ActiveRecord::Base
attr_accessible :approved, :entry_text, :reviewed, :user_id, :photo
has_attached_file :photo, :styles => { :small => "250x250", :large => "500x500>" },
:url => "link-to-s3-will-be-env-var",
:path => "path/:id/:basename.:extension"
process_in_background :photo
end
And then my little worker class (the part that doesn't seem to be run)
class PhotoProcessor
#queue = :paperclip
def self.perform(photo_id)
puts "*****************OMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMGOMG*********************"
puts photo_id.inspect
end
end
I can't get it to output the crazy string, just for testing purposes, but I can't even seem to have this method called. Thanks for any help, I am clearly missing something obvious.
I am using paperclip to upload photos in my two rails app which share a single database. Now the problem is, if I upload a photo in app-a, for instance, paperclip gives me a url as:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-a/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Here is what I set up in my item model:
class Item < ActiveRecord::Base
attr_accessible :photo, :photo_url, :poll_id, :brand, :number_of_votes
has_attached_file :photo,
:styles => { :thumbnail => "100x100#",
:small => "150x150>",
:medium => "250x250>",
:large => "400x400>" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:url=>"/item_:id/created_at_:created_at/:style.jpg"
Paperclip.interpolates :created_at do |attachment, style|
attachment.instance.created_at
end
end
In the other app app-b, when I query the url with item.photo.url(:large), it gave me:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-b/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Therefore I got a wrong url.
Are there any ways I can do that by configuring paperclip? Thanks!
As described here : https://github.com/thoughtbot/paperclip#defaults , you can change the default config, in your case I guess you want to change the :local_root so it doesn't include the app name.
The Problem:
I am getting an error message when submitting my form that says:
ActiveModel::MassAssignmentSecurity::Error in AdmissionRecordsController#create
Can't mass-assign protected attributes: admission_record
My Setup:
I am using Rails 3.2.3, with extra gems including Cocoon 1.0.14 and Simple_Form 2.0.2
The View:
My app/views/admission_records/_form.html.haml looks like:
= simple_form_for [#admission, #record] do |f|
= f.simple_fields_for :vital_signs, #record.vital_signs.build do |vs|
= render :partial => "vital_sign_fields", :locals => { :f => vs }
= link_to_add_association "Add Vital Signs", f, :vital_signs
= f.submit
And my app/views/admission_records/_vital_sign_fields.html.haml looks like:
.nested-fields
= f.label :sbp
= f.text_field :sbp
...
= link_to_remove_association "Remove Vital Sign"
What I am basically trying to do is that I have a resource called AdmissionRecord nested within another resource called PatientAdmission (route.rb shown below). I have another resource called VitalSign which I want to be able to create via a nested form (using cocoon and simple_form) when creating the AdmissionRecord
My config/routes.rb file looks like:
resources :patient_admissions do
resources :admission_records
end
The Models:
My app/models/patient_admission.rb looks like:
class PatientAdmission < ActiveRecord::Base
has_many :admission_records, :dependent => :destroy
end
My app/models/admission_record.rb looks like:
class AdmissionRecord < ActiveRecord::Base
belongs_to :patient_admission
has_many :vital_signs, :dependent => :destroy
accepts_nested_attributes_for :vital_signs, :rejects_if => :all_blank, :allow_destroy => true
attr_accessible :vital_signs_attributes
end
And my app/models/vital_sign.rb looks like:
class VitalSign < ActiveRecord::Base
belongs_to :admission_record
attr_accessible # just fields that appear in the form
end
The Controller:
The new and create methods in my AdmissionRecordsController looks like:
before_filter do
#admission = PatientAdmission.find(params[:patient_admission_id])
end
def new
#record = #admission.admission_records.build
end
def create
#record = #admission.admission_records.build(params[:admission_record])
#vital_sign = #record.vital_signs.build(params[:vital_signs])
#vital_sign.save
if #record.save
# Flash success and redirect to the right place
else
# Flash error and render :new
end
end
The Plea:
Please help me find where I'm going wrong. I've googled for hours and have looked at other examples and source code for demo apps such as those found in cocoon_simple_form_demo, but still can't seem to fix this error. If there's any other piece of information needed to debug this problem, please let me know. Thanks!
Okay I just had this problem and fixed it by entering one line of code in the belongs_to model.
# patient_admission.rb
Class PatientAdmission < ActiveRecord::Base
attr_accessible :admission_record_attributes
accepts_nested_attributes_for :admission_record
...
end
Here is another solution to it :)
I am trying to follow the tutorial Dynamic multiple image uploads with Ruby on Rails that creates a new model for photos and will associate them with another model. After copying and pasting all the code I'm getting an "AssociationTypeMismatch" error as follows.
ActiveRecord::AssociationTypeMismatch in AdminForksController#update
Photo(#2176152540) expected, got Array(#2148417160)
app/controllers/admin_forks_controller.rb:23:in `update'
{ "commit"=>"update",
"fork"=>{"position"=>"",
"name"=>"FORK",
"brand"=>"",
"photos"=>{"data"=>#<ActionDispatch::Http::UploadedFile:0x103634328 #headers="Content-
Disposition: form-data; name=\"fork[photos][data]\"; filename=\"marty.jpg\"\r\nContent-Type:
image/jpeg\r\n", #tempfile=#<File:/var/folders/cZ/cZVO8X55FeynZw5cHRz1UE+++TI
/-Tmp/RackMultipart20110716-18721-16ttjsd-0>, #content_type="image/jpeg",
#original_filename="marty.jpg">},
"authenticity_token"=>"iSzZxyzTe/LDLIf4cQiYBGLIk96INnKCP3SC5b5MXHw=",
"utf8"=>"?",
"id"=>"7"}
My forks model looks like this:
class Fork < ActiveRecord::Base
has_many :photos
accepts_nested_attributes_for :photos, :allow_destroy => true
end
And my photos model looks like this:
class Photo < ActiveRecord::Base
belongs_to :fork
has_attached_file :data
end
It looks like :photos is getting passed as an array, and :fork is expecting it. How do I resolve this issue?
Update
The issue was resolved (see the answer). However, how do the two variables, :fork and #fork, differ?
The problem was solved. In my for_for I was using :fork instead of #fork.
I've just started using Paperclip today and am having issues getting images to render. After some wrangling the photos are saving in the correct directory but there is a routing/rendering error:
ActionController::RoutingError (No route matches "/public/system/products/19/original/puppies-3.jpg")
However, the images are definitely saving in the correct directory. This is what's in my product model:
class Product < ActiveRecord::Base
validates :title, :presence => true
validates :description, :presence => true
validates :category, :presence => true
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
attr_accessible :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at
attr_accessible :title, :description, :category, :price
has_attached_file :photo, :styles => { :small => "150x150>", :large => "400x400>" },
:path => ":rails_root/public/system/products/:id/:style/:basename.:extension",
:url => "/system/products/:id/:style/:basename.:extension"
end
This is in my view:
<%= image_tag #product.photo.url %>
At the moment it's simply returning the image basename instead of the image itself, any thoughts? Products is available as a resource in routes.rb, but do I need to explicitly make photos available also somehow? I'm also fairly new to Rails so struggling a little bit...
Are you sure that product with that id has a image attached? I think some of your products dont have images attached but u still looking for them. Maybe try to set a default_url. Something like this:
has_attached_file :photo, :default_url => "/images/missing.png"
This will show that image set in default_url for the products what dont have images attached.
It's ok, I've fixed it. I had left a field in my form which was naming the photo_file_name from when I couldn't get the files to save and was trying to manually override it to get it to work (face-palm). It's amazing how much clarity comes from a few hours sleep.