add_column :issues, :data, :binary, :limit => 1.megabyte
get '/issues/:id/show_image' => 'issues#show_image', as: 'show_image_issue'
def show_image
#issue = Issue.find(params[:id])
send_data #issue.data, :type => 'image/png', :disposition => 'inline'
end
<%= f.file_field :data, as: :file %>
<%= image_tag show_image_issue_path(#issue) %>
The page loads and no errors, but there's no image.
I recommend for you to use PaperClip or CarrierWave for storing images, storing binary data has many disadvantages.
PaperClip: https://github.com/thoughtbot/paperclip
CarrierWave: https://github.com/carrierwaveuploader/carrierwave
why? read here : Storing Images in DB - Yea or Nay?
if you insist to do this then you can try:-
<img src="<%= #issue.data %>" />
Related
I have some problem with paperclip gem. When I try add record in my app i got error : http://postimg.org/image/xy0stdctd/
When i add record without image upload, everything works great.
controller
def create
#user = User.new
end
def made
#user = User.new(user_params)
if #user.save
redirect_to(action:'index' )
else
render ('index')
end
end
def user_params
params.require(:user).permit(:avatar, :name)
end
view
<%= form_for :user, url: {action: 'made'}, :html => { :multipart => true } do |form| %>
<%= form.text_field :name %>
<br>
<%= form.file_field :avatar %>
<br>
<%= form.submit "dodaj" %>
<% end %>
in terminal I have
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (8.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.0ms)
Rendered C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/
layout (130.0ms)
Cannot render console with content type multipart/form-dataAllowed content types: [#, #, #]
I'm creating a web application with formtastic. I installed the gem and wrote my code like this:
<%= semantic_form_for #index do |form| %>
<%= form.inputs do %>
<%= form.input :name %>
<%= form.input :born_on, :start_year => 1997 %>
<%= form.input :description, :as => :text %>
<%= form.input :female, :as => :radio, :label => "Gender", :collection => [["Male", false], ["Female", true]] %>
<% end %>
<%= form.actions do %>
<%= form.action :submit, :as => :button %>
<% end %>
<% end %>
I want the form to appear on the index page which is why I have #index. For some reason I can't do #index. How would I reference the top line so that it renders a form on the index page? Currently my index page has nothing in it, but it is defined in the controller
form_for helper expects some object that responds to fields You refer inside Your form. Here is an example of using semantic_form_for with plain Ruby object: http://affy.blogspot.com/2010/02/using-formtasic-without-activerecord.html.
Also the object You specify for form doesn't effect which page is being rendered. Are You sure You are not mixing up something? Maybe if You share a bit more of Your controller code we might help You better.
Whenever I have used paperclip to upload multiple images I have created a separate model called images for example and used the accepts_nested_attributes_for in the associated model..
But what if i want to add multiple images without using this. So for example if my model looks like this
class Portfolio < ActiveRecord::Base
belongs_to :sector
attr_accessible :overview, :title, :sector_id, :photo
has_attached_file :photo, :styles => { :portfolio => "680x680#"}
end
And my form looks like this
<%= form_for #portfolio do |f| %>
<%= f.label :title, "Title", :class => 'title_label' %>
<%= f.text_field :title %>
<%= f.label :sector_id, "Choose Sector", :class => 'title_label' %><br>
<%= f.collection_select(:sector_id, Sector.all, :id, :name, :prompt => "Please Select a Sector") %><br>
<%= f.label :overview, "Overview", :class => 'title_label' %>
<%= f.text_area :overview %><br>
<%= f.file_field :photo %><br><br>
<%= f.submit 'Submit', :class => 'btn' %>
<% end %>
How would i go about uploading multiple images in this scenario.. Normally within nested form for example i would use the link_to_add helper provided.
I don't have this available though with this scenario so what can i do?
is it best practive to always keep the attachments/photos separate and use nested_attributes?
Any help appreciated
To add multiple photos you need to mention, :html => { :multipart => true} in form tag as,
<%= form_for(#portfolio, :html => { :multipart => true}) do |f| %>
and in file_tag as,
<%= f.file_field :photo, as: :file, multiple: true, name: 'photo[photo]' %>
name photo[photo] so that each photo will get same name and can easily accessed in controller.
Just dtas it..
NOTE: i consider you are using jquery with paperclip to upload photos. If not try out this n let me knw if it works.. ;)
I am following the tutorial at http://www.noupe.com/ajax/create-a-simple-twitter-app.html
I am using ruby version 1.9.3p194 , rails version 3.2
When I created the view file index.html.erb , and put in the contents as
<b><%= render :partial => "message_form" %></b>
<%= render :partial => #posts %>
and then in the form partial , _message_form.html.erb
In partial _message_form
<% form_tag(:controller => "posts", :action => "create", :method=>"post") do %>
<%= label_tag(:message, "What are you doing?") %><br/>
<%= text_area_tag(:message, nil, :size => "44x6") %><br/>
<%= submit_tag("Update") %>
<% end %>
and load the index.html.erb , I only see the message "In partial _message_form" . the form tags do not render at all.
Need help. Would be glad to provide any additional required information
As far as I remember you need this: <%= form_tag...
UPDATE: There was a fallback to previous behavior (without =), but it doesn't seem to exist in Rails 3.2.6
Im stuck uploading multiple photos to my model when adding an extra field to the photo model. In the code i hard code "1" value to the hidden_field, but i will change it eventually. The paperclip gem raises a rollback and won't insert the photos in the post. If i erase the "hidden_field" line it will success. Any ideas on how to add extra field to the upload in the view?
<%= form_for #campaign_point_of_sale, :html => {:multipart => true }, :url => "/pos/#{#point_of_sale.id}/post/#{#campaign.id}", :method => :post do |f| %>
<%= f.hidden_field :id %>
<label>Add photo <br />
<%= f.fields_for :campaign_result_point_of_sale_photos do |builder| %>
<% if builder.object.new_record? %>
<%= builder.hidden_field :is_mount_photo, :value => "1" %>
<%= builder.file_field :photo %>
<% end %>
<% end %>
<%= f.submit(:value => "Save") %>
Got it!
Just needed to delete the whole builder with both hidden_field and file_field, so the server would not try to insert a picture without an image file (just with a hidden_field value)
This can be achivied by surrounding both inputs with a div and deleting it (with jquery) at form submit if the file_field value was empty. Pretty cheap but works!