I've done the migration I have
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/missing/:style.png"
in my model, and im using
<%= form_for #user, :as => :user, :url => user_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<div class="actions">
<%= form.submit %>
</div>
To save the avatar uploaded and
<%= image_tag #user.avatar.url %>
<%= image_tag #user.avatar.url(:medium) %>
<%= image_tag #user.avatar.url(:thumb) %>
To display them all, but when I submit the new avatar it is not getting saved, I'm using
#user = User.create(params[:user])
in the create so it should be saving, what could be the problem?
<%= form_for #user, :validate => true, :html => { :multipart => true } do |f|%>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username, :disabled => 'disabled' %><br />
<%= f.label :full_name %><br />
<%= f.text_field :full_name%><br />
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<%= f.file_field :avatar %>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
ruby-1.9.2-p290 :002 > user = User.first
ruby-1.9.2-p290 :004 > user.avatar
=> /images/missing/original.png
Nothing is getting changed
avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>
You need to add :avatar to the attr_accessible list.
I assume that everything else for user is being saved properly?
The only thing that comes to my mind is, maybe you're using attr_accessible to protect from mass assignment and you forgot to add :avatar_file_name, :avatar_content_type, :avatar_file_size... to the whitelist?
Related
I'm trying to read the exif data from jpeg images that I upload with paperclip using the exifr gem and save focal length, etc to attributes but I can't seem to do it. I'm a rails beginner and would be most appreciative if anyone could assist.
my photo.rb model:
class Photo < ActiveRecord::Base
attr_accessible :date, :exif, :name, :photo
has_attached_file :photo, :styles => { :small => "200x200#" , :medium => "1280x720#" },
:url => "/assets/photos/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/photos/:id/:style/:basename.:extension"
after_photo_post_process :load_exif
validates_attachment_presence :photo
validates_attachment_content_type :photo, :content_type => ['image/jpeg']
def load_exif
exif = EXIFR::JPEG.new(photo.queued_for_write[:original])
return if exif.nil? or not exif.exif?
self.exposure = exif.exposure_time.to_s
self.f_stop = exif.f_number.to_f.to_s
self.focal_length = exif.focal_length.to_f.round.to_s
self.iso = exif.iso_speed_ratings
self.date = exif.date_time.to_date
rescue
false
end
end
my form (using SimpleForm):
<%= simple_form_for #photo, :html => {:multipart => true} do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.file_field :photo, :label => 'Attach photo' %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
show.html.erb with fields I'm trying to show:
<p>
<b>Name:</b>
<%= #photo.name %>
</p>
<p>
<b>Date:</b>
<%= #photo.date %>
</p>
<p>
<b>Exposure:</b>
<%= #photo.exposure %>
</p>
I'm probably missing something stupid so I apologize if that's so.
Try changing the first line in your load_exif method to:
exif = EXIFR::JPEG.new(photo.queued_for_write[:original].path)
Without .path on the end, I don't believe it's returning the file path.
I'm using accepts_nested_attributes_for in one of my Rails app. i have three models :album has_many :photo and each :photo has_and_belongs_to_many :tag. User can add more photo in album using paperclip and jquery.multifile.js. every thing working fine but the main problem is the hash is not properly create.
album => {:name => "", :body => "", :photos_attributes => {"tags_attributes" => {:name => "abc"}, "photo" => {"file" => "tempfile"}}}
but i need.
album => {:name => "", :body => "", :photos_attributes => "photo" => {"file" => "tempfile", "tags_attributes" => {:name => "abc"}}}}
My view is: please consider the gallery => album and attachment => photo
<%= form_for(#gallery, :html => {:multipart => true}) do |f| %>
<div id = "gallery_name_formField">
<%= f.label "Gallery Name:" %><br />
<%= f.text_field :name, :name => "gallery[name]" %><br />
</div>
<div id = "gallery_body_formField">
<%= f.label "Gallery Description:" %><br />
<%= f.text_area :body, :name => "gallery[body]" %><br />
</div>
<%= f.fields_for :attachments do |af| %>
<div id = "gallery_file_formField">
<%= af.label "Select The Attachment:" %><br />
<%= af.file_field(:attachment, :name => "gallery[attachments_attributes][][attachment]")%>
</div>
<div id="tags">
<%= af.fields_for :tags do |tf| %>
<%= tf.label :name, "Tag:" %><br />
<%= tf.text_field :name, :id => "mySingleField_0", :name => "gallery[attachments_attributes][][tags_attributes][][name]" %>
<% end %>
</div>
<% end %>
<div class="attachment_submit_formField">
<%= f.submit(:id => "create_gallery") %>
</div>
<% end %>
Have you tried to use nested form gem? Give it a try
Its very straightforward way to have multiple nested forms.
It works for me.
I'm using Simple Form. I have a form for creating new items, and a form for editing existing ones. I also have two file fields for every item. Thing that bugs me is that file fields are displayed fine when creating new item, but then they are not generated at all when editing an existing item.
I had this perfectly working in Rails 3.0, now doesn't work on Rails 3.2.1.
The form:
<%= simple_form_for #item, :html => { :multipart => true } do |f| %>
<%= f.input :title, :input_html => { :maxlength => 35 } %>
<%= f.input :description, :input_html => { :maxlength => 450 } %>
<%= f.input :secure_details, :placeholder => "Serial numbers and other stuff that will remain private", :input_html => { :maxlength => 450 } %>
<%= f.association :bookmark, :collection => current_user.bookmarks(:order => :position), :include_blank => false %>
<%= f.input :warranty_until, :as => :string, :input_html => { :id =>'datepicker2' } %>
<div class="image_attachment">
<div class="attachment_text">
Update item photo<br />
<small>(will replace old one)</small>
</div>
<div class="attachment_button">
<% f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
</div>
</div>
<div class="image_attachment">
<div class="attachment_text">
Update receipt<br />
<small>(will replace old one)</small>
</div>
<div class="attachment_button">
<% f.fields_for :receipts do |receipt| %>
<%= receipt.file_field :photo %>
<% end %>
</div>
</div>
<%= f.input :public, :label => "My friends can see this item", :input_html => { :class => "right" } %>
<%= f.input :giveaway, :label => "Mark as giveaway", :input_html => { :class => "right" } %>
<div class="margin_r margin_t">
<%= f.button :submit, :class => 'small_button white right' %>
</div>
<% end %>
Basically this part of code doesn't work:
<div class="attachment_button">
<% f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
</div>
The generated HTML is just empty div.
The very same code works when creating a new item, but doesn't work when editing existing one.
Both Assets and Receipts are using Paperclip for storing images. Here is a code for Asset class:
class Asset < ActiveRecord::Base
belongs_to :item
has_attached_file :photo,
:styles => {
:thumb => "80x80#",
:small => "150x150>" }
validates_attachment_size :photo, :less_than => 550.kilobytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
end
Maybe you forgot add this line of code in your Item model:
accepts_nested_attributes_for :receipts, :allow_destroy => true
accepts_nested_attributes_for :assets, :allow_destroy => true
And add '=':
<%= f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
<%= f.fields_for :receipts do |receipt| %>
<%= receipt.file_field :photo %>
<% end %>
I have successfully installed Paperclip gem and uploaded a picture to my post, but in show action there isn't any picture, just alt text with image alt.
I am using ruby on rails 3.2.1
#post form:
<% create_url = {:url=>{:action=>"create"}} if #post.new_record? %>
<% form_for #post, :html => { :multipart => true } do |t| %>
<b><%= t.label :title, 'Virsraksts:' %></b><br />
<%= t.text_field :title %><br /><br />
<b><%= t.label :content, 'Teksts:' %></b><br />
<%= t.text_area :content %><br /><br />
<%= f.file_field :bildes %>
<div class="actions">
<%= t.submit %>
</div>
#post show:
<p><%= #post.content %></p>
<%= image_tag #post.bildes.url %>
<%= image_tag #post.bildes.url(:medium) %>
<%= image_tag #post.bildes.url(:thumb) %>
In my opinion #post model url and path is not right, but exactly what I don't know.
#post model:
class Post < ActiveRecord::Base
has_attached_file :bildes, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:url => ":rails_root/app/assets/bildes/:id/:style/:basename.:extension",
:path => ":rails_root/app/assets/bildes/:id/:style/:basename.:extension"
end
Try using the default url/path for the attached file by not specifying it in the model.
I am trying to use the client_side_validations gem with Devise to validate devise registrations form.
Validations work fine with everything else just not the Devise generated form.
I added the relevant :validate => true but the validations only work when I hit submit not on tab like they do on every other form.
<h2>Sign up</h2>
<hr />
<%= form_for(resource, :validate => true, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :username %>
<%= f.text_field :username %></div>
<div><%= f.label :email %>
<%= f.email_field :email %></div>
<div><%= f.label :password %>
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %></div>
<br />
<div><%= f.submit "Sign up", :class => "btn btn-primary btn-large" %></div>
<% end %>
<%= render "links" %>
Argc, argv! I am using Rails 3.2.1, the latest release of the gem is incompatible with 3.2 hence the nightmare. Using 3.2.0.beta.2 fixes the problems. Thanks!
Try to put the :validates => true on your fields directly, like this :
<h2>Sign up</h2>
<hr />
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div>
<%= f.label :username %>
<%= f.text_field :username, :validate => true %>
</div>
<div>
<%= f.label :email %>
<%= f.email_field :email, :validate => true %>
</div>
<div>
<%= f.label :password %>
<%= f.password_field :password, :validate => true %>
</div>
<div>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, :validate => true %>
</div>
<br />
<div>
<%= f.submit "Sign up", :class => "btn btn-primary btn-large" %>
</div>
<% end %>
<%= render "links" %>
change the line
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
to
<%= form_for(#user, :url => registration_path(resource_name), :validate => true) do |f| %>
I haven't used client_side_validations gem extensively yet. But from the look of it, it needs to have data-validate="true" in the form (and form elements) tags.
Do you find it in the output html form like:
<form novalidate="novalidate" method="post" data-validate="true" action="/some_actions" >
If you don't find it, you might want to write your form_for like this:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), {:validate => true}) do |f| %>
Does it help?
To use an stable version use 3.0.3 that was the latest stable version supporting rails 3.2.x