How to update paperclip attachment field record - ruby-on-rails

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

Related

Custom parameters from view to controller

i'm creating a form where a user can choose a product and a quantity. I need to pass the id value from the object #event to the controller, but i dont know whats the right way to do it. As it is now, it the params[:event_id] field is always nil in the controller.
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
Add hidden filed in your form and set its value equal to #event.id
<%= form_tag logic_giveRandomGifts_path :method => 'post' %>
<div class="form-group">
<%= collection_select(:params, :product_id, Product.all, :id, :name, :prompt => true) %>
Quantidate:
<%= text_field_tag :quantity, params[:quantity], :size => 2 %>
<%= hidden_field_tag :event_id, value: #event.id%> #add this
<%= submit_tag "GO!",params[:event_id] => #event.id,:class => 'btn btn-default' %>
</div>
<% end %>
You can use now event id in your controller as params[:event_id]
<%= hidden_field_tag :event_id, #event.id %>
So this will be available in params[:event_id].
Simply pass an hidden field in form as followings
<%= hidden_field_tag :event_id, value: #event.id%>
It will available in controller as
params[:event_id]

Carrierwave-direct: Saving Image with Default Values

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?

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.

Submitting a Form from index page in Rails

I'm Rails newbie.
How to make a form which allows user to choose a language(en,fr etc) through Radio buttons in Home#Index View to Submit to Home#Language action ?
Thanks in advance
<%= form_tag language_path, :method => :post do %>
<%= label_tag :language_english, 'English' %>
<%= radio_button_tag :language, 'english' %>
<%= label_tag :language_french, 'French' %>
<%= radio_button_tag :language, 'french' %>
<%= submit_tag %>
<% end %>
Where language_path is the path defined in your routes.rb, such as
match "/home/language" => "home#language", :as => 'language'

rails simple_form two models

I'm starting to use simple_form for a rails application, and while converting some of my forms, I came across one that has two models that it is working with, sort of an embedded form. Is this possible with simple_form?
<% simple_form_for :topic, :url => forum_topics_path do |t| %>
<%= t.input :name, :label => 'Topic' %></p>
<p>First Post:<br/></p>
Title: <%= text_field :post, :title %> <--- this is where i start having problems
Body: <%= text_area :post, :body %>
<%= t.submit 'Save' %>
Thanks
Use simple_fields_for :
<%= simple_form_for :topic, :url => forum_topics_path do |topic_builder| %>
<%= topic_builder.input :name, :label => 'Topic' %>
<%= topic_builder.simple_fields_for :post do |post_builder| %>
<p>First Post:</p>
<%= post_builder.input :title, :input_html => { :size => 30 } %>
<%= post_builder.input :body, :as => :text, :input_html => { :rows => 20, :cols => 50, :class => 'resizable' } %>
<% end %>
<%= topic_builder.submit 'Save' %>
<% end %>
Notes
Note the = symbol in <%= simple_form_for ... and <%= simple_fields_for (required in Rails 3.x)
Removed "Title:" and "Body:" text. Use the label generated for the inputs and style their location with CSS as needed.
Added example of using input_html
There's another approach that I'm using and it works great. Ryan Bates (RailsCasts) has created a gem to handle this.
See https://github.com/reu/simple_nested_form for the details.

Resources