Carrierwave-direct: Saving Image with Default Values - ruby-on-rails

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?

Related

how to get an image to show in a formtastic :radio, :collection?

I can't get an image to show in a Formtastic form with radio buttons. Here's my form. The :activity_id, :as => radio is the issue:
<%= semantic_form_for #event do |f| %>
<%= f.inputs do %>
<%= f.input :date, :as => :string, :input_html => { :class => 'jquery-ui-date'} %>
<%= f.input :date, :as => :hidden, :input_html => { :id => 'date-alt'} %>
<%= f.input :activity_id, :as => :radio, :collection => Activity.all %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :button %>
<%= f.action :cancel, :as => :link %>
<% end %>
<% end %>
That works fine as far as showing the radio button, the activity name, and saving correctly on submit. But I need to show the image that goes with the activity because most of my users cannot read and must rely on images and TTS. I've got the images showing in my other views.
I've tried dozens of combinations of things with :hint, and :wrapper_html, and image_tag and so on. So many variations I've become scrambled in the brain-pan.
A sample attempt:
<%= f.input :activity_id, :as => :radio, :collection => Activity.all, :hint => f.template.image_tag(f.object.image_url(:thumb)) %>
This gives me an "undefined method `image_url'" error. Yet this :hint works fine elsewhere.
I'll switch back to a standard html.erb view if that helps, but my problem there was a "stringify_keys" error I was unable to solve.
There's gotta be a way. Please? Thanks much. . .

How to update paperclip attachment field record

I created an edit form for profile page which contain First Name, Last Name and Photo
<%= form_tag "/profiles/update", :html => {:multipart => true} do%>
<h4>First Name:</h4>
<%= text_field_tag :first_name, #profile.first_name %>
<h4>Last Name:</h4>
<%= text_field_tag :last_name, #profile.last_name %>
<h4>Photo</h4>
<%= file_field_tag :photo %>
<%= submit_tag "Save changes" %>
<% end %>
and on update action I put
#profile.update_attributes(:first_name => params[:first_name], :last_name => params[:last_name], :photo => params[:photo])
So when I run edit form and select new image for file field then submit I got this Error
Paperclip::AdapterRegistry::NoHandlerError in ProfilesController#update
No handler found for "Pic.jpg"
Any suggestions what's the problem here?
It should be:
<%= form_tag "/products/create", :multipart => true do%>
It will work fine now

Updating paperclip avatar in multipart simple_form

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.

Rails 3.1 Formtastic input custom id or class

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" } %>

How to pre-check checkboxes in formtastic

I have a form I'm trying to set up ...
Users can have many posts, and each post can have many people watching it.
The Watch model is set up polymorphically as 'watchable' so it can apply to different types of models. It has a user_id, watchable_id, watchable_type and timestamps as attributes/fields.
This is soley so that when people comment on a post, users watching the post can get an email about it.
What I'm trying to do is show the user a list of users that they can tag on each post, which is not problem. This is what I'm using right now
http://pastie.org/940421
<% semantic_form_for #update do |f| %>
<%= f.input :subject, :input_html => { :class => 'short' } %>
<%= f.input :site, :include_blank => false, :input_html => { :class => 'short' } %>
<label>Tag Users (they will get notified of this update)</label>
<%= f.input :user, :as => :check_boxes, :label => ' ', :wrapper_html => { :class => "radiolist clearfix" }, :for => :watch, :name => "Watch" %>
<%= f.input :note, :label => 'Update'%>
<% f.buttons do %>
<%= f.commit_button :button_html => { :class => 'submit' } %>
<% end %>
<% end %>
The problem with this, is that when you go to edit an update/post ... all the checkboxes are prechecked ... I want it to pre-check only users who are currently watching the post.
To further clarify ... this is the hacky way I'm getting it to do what I want right now
<ul class="radiolist clearfix">
<% #users.each do |user| %>
<li>
<%= check_box_tag 'user_ids[]', user.id, #watches.include?(user.id) ? true : false -%>
<%= h user.name -%>
</li>
<% end %>
</ul>
where #watches is just an array of user ids
#watches = #update.watches.map{|watcher| watcher.user_id}
For anyone else having the same issue:
<%= f.input :some_input, :as => :boolean, :input_html => { :checked => 'checked' } %>
For multiple check boxes, this way:
<%= f.input :tags, :as => :check_boxes, :collection => some_map.collect { |c| [c[:name], c[:id], {:checked=> tag_ids.include?(c[:id])}] } %>
If you need the state of the checkbox to reflect the value of :some_input
<%= form.input :some_input, :as => :boolean, :input_html => { :checked => :some_input? } %>
In your model..
def some_input?
self.some_input ? true : false
end
Set the boolean attribute's value to 'true' in the controller before your render the form. That should make Formtastic default the checkbox to 'checked'.

Resources