Edit and destroy image paperclip rails - ruby-on-rails

I have problem with paperclip gem. I'm using Rails 4. I would like to make possiblity for user, to edit image attachment. For now when I'm editing Speaker, I can only upload new image, no edit existing one.
= simple_form_for(#speaker) do |f|
= f.error_notification
.form-inputs
= f.input :first_name
= f.input :nickname
= f.input :last_name
= f.input :description
- if #speaker.image.exists?
= image_tag #speaker.image.url(:medium)
= f.input :delete_image, as: :fake
= #speaker.image = nil
= #speaker.save
= f.input :image
.form-actions
= f.button :submit
However, I can't create checkbox input with :delete_image, so for now, with every refresh site it destroys image (because of #speaker.save).
Could give me some advice, how to fix it ? Solutions from Rails 3 cant help me.

In your controller
def remove_picture
speaker = Speaker.where(id: params[:id]).first
speaker.image.destroy
redirect_to request.referer
end
in your view create following link -
- if #speaker.image.exists?
= link_to "Remove Image", remove_image_path(#speaker), method: :delete
in your routes.rb define routes for same
delete'/controller/remove_image/:id' => 'controller#remove_image', as: :remove_image
Try above code.

Do you mean you want the user to be able to edit the image like they would in Photoshop, or similar (perhaps more simple) image editing apps? If so then your best option is to use a javascript plugin, eg one of these jQuery plugins:
http://www.sitepoint.com/image-manipulation/
http://www.jqueryrain.com/2014/11/picedit-jquery-front-end-image-editor-plugin/
These were got just by googling "jquery image editor plugin".
See also this related question: Javascript image editing plugin
You will need to choose which one is the best fit for you based on the types of editing options you want to give your user.

Related

How to set field automatically in Rails

I have a simple feedback form to allow the Users send the feedback. Controller for it:
class FeedbacksController < ApplicationController
expose(:feedback, attributes: :feedback_params)
def create
ApplicationMailer.customer_feedback(feedback).deliver_now if feedback.save
respond_with(feedback, location: root_path)
end
private
def feedback_params
params.require(:feedback).permit(:email, :message, :name, :phone)
end
end
Here I'm using expose gem.
And the view for it:
= simple_form_for(feedback, url: feedback_path) do |f|
legend
= t(".any_questions")
.form-inputs
= f.input :name
= f.input :email
= f.input :phone
= f.input :message, as: :text
.form-actions
= f.button :submit, t(".submit")
I want the next: to check somewhere if current user present(logged in to application) If yes = then set the email field using user.email (when he visit the feedback form -> email should be already filled in. If user not logged in to site then this field should be empty. Where should I put this logic? Thanks!
The simple form documentation explains how simple form is very easy to customize according to your needs. Assuming you are using devise for user authentication (if you're not, simple_form is designed to work with devise so you should check it out), then you can user current_user to access the currently logged in user.
I think simple_form's input_html would help you here:
= f.input :email, input_html: { value: "#{current_user.email if current_user}"}

Cocoon how to remove associations

I am trying to use the Cocoon gem to add/remove assets on asset_profiles. Everything up to this point works, I am just having a problem with link_to_remove_association. When the button is clicked, the field is removed. But if the field had been filled in and saved previously, I am unable to remove said association. The button just hides the field until I hit update. Is there a way to actually remove the association from the database through link_to_remove_association? Any help is much appreciated. Thanks in advance.
Here is the code I am referring to:
Asset.show
= simple_form_for([#asset_profile, #asset]) do |f|
= f.input :max_users, as: :hidden
#assets_users
= f.simple_fields_for :assets_users do |assets_user|
= render "assets_user_fields", f: assets_user
.links
= link_to_add_association "Add Another User", f, :assets_users
= f.submit
Asset._assets_users_fields
.nested-fields
= f.input :user_id, collection: #users.order(:last_name), :label => "User"
= link_to_remove_association "Remove", f
Screenshot of page pre-remove:
Screenshot of post-remove:
Screenshot of post-update(page reload):
I would much rather, after the update, the page be reloaded and looking like below, which is the form for initially adding users to an asset:
When using strong parameters, you have to make sure to permit :id and :_destroy, as documented by the way (see documentation ).
It is not unlogical: cocoon sets the _destroy if something needs to be removed, rails then needs the id to know what to remove.
Im my case, i forget for allow_destroy: true.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
Did you permit the nested parameter _destroy in either permit (strong_parameters) or attr_acceptible?
Your asset_profile_params should look like
def asset_profile_params
params.require(:asset_profile).permit(:max_users, :asset_users_attributes => {:user_id, :_destroy})
end

Rails direct upload to Amazon S3 using Activeadmin + Paperclip

I am using Activeadmin and Paperclip to make images upload on my Rails app. When I try to upload big files to S3 the timeout error occurs, so I have to implement the direct upload to S3.
Does anyone know how can I make it? I could't figure it out...
There is a really nice article I've used when was first time setting up the AA+s3+Paperclip.
It has decent explanations + example app on Github, so you can check it live.
In AA the form would look something like this:
form multipart: true do |f|
# f.semantic_errors *f.object.errors.keys
f.inputs do
f.input :image_name #or whatever field is called
end
f.has_many :attachments do |a|
if a.object.persisted?
link_to image_tag(a.object.encoded_url, class: 'image-preview'), a.object.encoded_url, target: "_blank"
else
a.inputs do
a.s3_file_field(:attachment, as: :file, class: 'js-s3_file_field')
end +
a.inputs do
a.input(:s3_url, as: :hidden, input_html: { class: "s3_url" })
end
end
end
f.actions
end
The answer appears to be in the comments. Thanks Andrey for the tutorial link.
http://blog.littleblimp.com/post/53942611764/direct-uploads-to-s3-with-rails-paperclip-and

Submitting multiple forms in Rails

I have to submit multiple forms, I followed the advice of this post: How to submit multiple, duplicate forms from same page in Rails - preferably with one button
Note I'm still quite new to Rails/programming and some of my ways of doing things might not be ideal.
Here's my view:
= form_tag ([#event, #registration]) do
- x.times do
= render 'multi_form'
= submit_tag "Submit registrations"
The form (note that there are more fields):
- hidden_field_tag :event_id, :value => #event.id
.control-group
= label_tag :title
.controls
= select("registrations[][title]", :registration, Registration::TITLE)
.control-group
= label_tag :first_name
.controls
= text_field_tag "registrations[][first_name]"
.control-group
= label_tag :last_name
.controls
= text_field_tag "registrations[][last_name]"
.control-group
= label_tag :email
.controls
= text_field_tag "registrations[][email]"
The controller:
def create
array_number = 0
x.times do
#registration = Registration.new(params[:registrations][array_number])
#registration.save
UserMailer.registration_user_notify(#event, #registration).deliver
array_number = array_number + 1
end
respond_to do |format|
format.html {redirect_to thank_you_event_registrations_path(#event)}
end
end
When submitting it seems, to an extent, to be doing the right thing, for one it fires off an email to x unique email addresses, which makes me think that #registration contains the correct details in each loop - it's not saving to the database however. I can see that all the params are there in the log file, except that :title seems to be doing something bad (see below: but I'll focus on that later), the main thing I want it to do now is run though each array and save it as a new entry.
The log:
Parameters: {"utf8"=>"รข", "authenticity_token"=>"BQXm5fngW27z/3Wxy9qEzu6D8/g9YQIfBL+mFKVplgE=", "event_id"=>"7", "registrations"=>[{"title"=>{"registration"=>"Mrs"}, "first_name"=>"Person", "last_name"=>"One", "email"=>"charl#privatelabel.co.za"...
I'm hoping the info I provided is enough, any advice will be appreciated.
Thanks!
EDIT:
#iblue
It did the trick! It was a validation error and it's saving everything into different rows. Thank you very much!
One more thing if I may, any idea how the :title form part should be formatted in order to return paramater:
"title"=>"Mrs",
as opposed to:
"registrations"=>[{"title"=>{"registration"=>"Mrs"},
Thanks again!
You are not checking if #registration.save actually saves the record. It can return true or false. I guess it just silently fails.
If you use #registration.save!, it wile raise an exception when anything goes wrong. I guess there is some kind of validation error there.

Selecting a default picture with radio button in a new nested attributes form

I've got a Rails project which has a car model and it has multiple pictures. I've created a nested form so I can input the cars details and pictures all at the same time. I would like a radio button to select the picture to display in the gallery.
%table
= simple_form_for [#car] :html => {:multipart => true} do |f|
= f.error_messages
= f.input :make
= f.input :model
= f.input :engine
= f.input :price
= f.simple_fields_for :car_pictures do |builder|
.picture
= builder.input :caption
= builder.input :picture
.thumb= image_tag builder.object.picture(:thumb)
= f.button :submit
Is it possible to use a radio button in the 'fields_for' section to select the default/main picture? Or is there a better way?
Update
By putting the following under the fields_for all the radio buttons work independently, which is not what I want.
= builder.radio_button(:favourite, "test")
Each radio button is getting a unique name.
Update
What if I used the object_id as the value, how would I go about linking this in the create method?
= f.radio_button(:favourite_pic, builder.object_id)
Yes, your last guess was correct. I made a test application in which the customer can select a favorite photo: https://github.com/eicca/attrs_test (see commit changes marked by "added favorite example").
There are two main points (according to your situation):
= f.radio_button(:favorite_pic_id, builder.object.id)
and
#models/car
has_one :favorite_pic, :class_name => "CarPicture"
And you need to add favorite_pic_id field to your cars table.
Why not? :) Only selected radio button will be sent to the server. So, only thing you've got to do - is to give it proper name/value and make sure that you have an accessor for it to support mass-assignment.

Resources