I would like to create an edit page for the below form. The problem is that when the user browses to the edit page the brand_name and name are pre-filled, but the image upload field shows 'no file chosen' even when an avatar exists for the 'style'. Please let me know if there is some way to remedy this. Thanks!
My Edit Form:
<%= simple_form_for #style, :html => { :class => 'form-horizontal' }, :remote => true do |m| %>
<%= m.input :brand_name, :label => 'Brand', :placeholder => 'Brand' %>
<%= m.input :name, :label => 'Style', :placeholder => 'Style' %>
<%= m.input :avatar, :label => "Image" %>
<div class="form-actions" style = "background:none">
<%= m.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', styles_path, :class => 'btn' %>
</div>
<% end %>
Just implemented this yesterday. Make a custom input in /app/inputs
class AvatarInput < SimpleForm::Inputs::FileInput
def input
out = '' # the output string we're going to build
# check if there's an uploaded file (eg: edit mode or form not saved)
if object.send("#{attribute_name}?")
# append preview image to output
# <%= image_tag #user.avatar.url(:thumb), :class => 'thumbnail', id: 'avatar' %>
out << template.image_tag(object.send(attribute_name).url(:thumb), :class => 'thumbnail', id: 'avatar')
end
# append file input. it will work accordingly with your simple_form wrappers
(out << #builder.file_field(attribute_name, input_html_options)).html_safe
end
end
Then you can do
<%= f.input :avatar, :as => :avatar %>
This is all I needed for this to work (in haml):
=simple_form_for #user, :html => {:multipart => true } do |f|
=f.file_field :image
The code for new/edit views from paperclip's github page looks like this:
<%= form_for :user, #user, :url => user_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
So maybe you should try m.file_field and include :html => { :multipart => true } as well? Though I personally prefer Attachment-Fu.
Related
I am using paperclip in my rails app
my form is
<%= form_for #portfolio_photo, :url => portfolio_photo_uplaod_individual_profile_path(:profile_id => current_individual.profile.id), :method => :POST, :html => { :multipart => true } do |f| %>
<%= f.hidden_field :profile_id, :value => current_individual.profile.id %>
<%= file_field_tag :portfolio_photo, multiple: true %>
<%= f.submit "submit" %>
<% end %>
and controller action is
def portfolio_photo_uplaod
#portfolio_photo = IndividualPhoto.create(portfolio_photo_params)
if #portfolio_photo.save
redirect_to individual_profile_path(current_individual)
end
end
and the strong parameters are
def portfolio_photo_params
params.permit(:portfolio_photo, :profile_id)
end
individual_photo.rb
class IndividualPhoto < ActiveRecord::Base
belongs_to :profile
has_attached_file :portfolio_photo, :styles => { :medium => "300x300>" }
validates_attachment_content_type :portfolio_photo, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
profile.rb
has_many :individual_photos
i am able to save when i upload a single image but i am not able to save multiple images instead only one image is saving in the database when i upload multiple images
Please help !!
Here is the answer for your current system:
In your form:
<%= form_for #portfolio_photo, :url => portfolio_photo_uplaod_individual_profile_path(:profile_id => current_individual.profile.id), :method => :POST, :html => { :multipart => true } do |f| %>
<%= f.hidden_field :profile_id, :value => current_individual.profile.id %>
<%= file_field_tag 'portfolio_photos[]', multiple: true %>
<%= f.submit "submit" %>
<% end %>
In your controller:
def portfolio_photo_uplaod
portfolio_photo_params[:portfolio_photos].each do |photo|
IndividualPhoto.create(portfolio_photo: photo, profile_id: portfolio_photo_params[:profile_id])
end
redirect_to individual_profile_path(current_individual)
end
and the strong parameters:
def portfolio_photo_params
params.permit(:profile_id, :portfolio_photos => [])
end
With your current design, above solution should work but it's not the best approach yet, you have some better ways to achieve this e.g. using accepts_nested_attributes_for to update Profile's photos through Profile Controller.
I have a Rails app where I can upload images to Amazon S3. This is done through Carrierwave via a form with many hidden values for saving particular attributes about the image (such as the project it's associated with, etc.):
Current Image Uploading Form
<%= semantic_form_for Image.new do |image_form| %>
<%= image_form.input :project_id, :as => :hidden, :label => false, :input_html => {:value => #project.id} %>
<% if #step.new_record? %>
<%= image_form.input :step_id, :as => :hidden, :label => false, :input_html => {:value => -1} %>
<%= image_form.input :saved, :as => :hidden, :label => false, :input_html => {:value => false} %>
<% else %>
<%= image_form.input :step_id, :as => :hidden, :label => false, :input_html => {:value => #step.id} %>
<%= image_form.input :saved, :as => :hidden, :label => false, :input_html => {:value => true} %>
<% end %>
I'm looking into moving the image uploading to a background task. I understand that if I use Carrierwave-direct, then I need to have the image submitted in its own direct_upload_form, which can't pass any of the hidden values I currently have. I also know that when the image has been successfully sent to Amazon S3, there is the success_action_redirect method I can call to redirect to a URL:
New image upload form
<%= direct_upload_form_for #uploader do |f| %>
<p><%= f.file_field :image_path %></p>
<p><%= f.submit "Upload Image" %></p>
<% end %>
New Controller method for image uploading
def index
#uploader = Image.new.image_path
#uploader.success_action_redirect = update_image_attributes_url
end
What I'd like to do is have the image automatically save with all the different hidden values I had in my original form (without redirecting to a page where I would need to manually fill out a form and click submit).
The way I'm thinking of doing this is to redirect to a view with a form for editing the particular image record and automatically trigger the submit button when the page is loaded. But I don't want the user to see a blank page when they're redirected to this page.
How can I have the success_action_redirect trigger some AJAX after the image has been uploaded to Amazon S3? Would something like this work?
<%= direct_upload_form_for #uploader, :remote=> true do |f| %>
<p><%= f.file_field :image_path %></p>
<p><%= f.submit "Upload Image" %></p>
<% end %>
Or is there a better way to do this generally?
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 want to make chain selects part inside my Formtastic form. But is it possible to set custom ids for multiple selects for future AJAX replacement?
This doesn't work:
<%= semantic_form_for [:admin, #production_year] do |f| %>
<%= f.inputs do %>
<%= f.input :car_model, :label => "CarModel", :as => :select, :collection => Brand.find(:all, :order => "name ASC"), :id => "brand_id" %>
<% end %>
<% end %>
Options should be passed to the input_html hash, like so:
<%= f.input :car_model, :input_html => { :id => "brand_id" } %>
I use form_for to save a model object site
<% form_for :site :url => {:controller => 'site', :action => 'add_site' } do |f| -%>
<%= f.text_field :protocol, :size => 127, :style => 'width:255px' , :value => "http://"%>
<%= f.text_field :site_name, :size => 127, :style => 'width:255px' , :value => "www."%>
<%= f.hidden_field :user_id, :value => #user.id %>
<%= f.hidden_field :status, :value => 'Not Verified' %>
<% end -%>
here the field protocol is not a instance of the model site. but i just want to pass to the action add_site so that i can use as params[:protocol]
what should i do to achieve this?
You can set something like this:
<%= text_field_tag :protocol %>
To the action of the controller you can refer to this as params[:protocol]
Add it to your site model as an attribute accessor:
attr_accessor :protocol
See http://railscasts.com/episodes/16-virtual-attributes .