ruby on rails photo upload problem - ruby-on-rails

Hallo
rails version 2.3.5
I'm learning rails and I run into a problem.
I'm doing some nesting forms from the railscasts tutorials. I changed the text area into a data field to upload photos and everything is working.
Now i have to display the uploaded pictures and i simply can't do it. I Tried everything I could find on the net but nothing worked.
PROBLEM
I have the Article controller which handles the article CRUD.
inside the article new form there is nested a form for uploading images.
article controller
def code_image
#image_data = Photo.find(params[:id])
#image = #image_data.binary_data
send_data(#image, :type => #image_data.content_type,
:filename => #image_data.filename,
:disposition => 'inline')
end
photo model
def image_file=(input_data)
self.filename = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.binary_data = input_data.read
end
articles/show.html.erb
<%=h #article.title %>
<%=h #article.body %>
<% for photos in #article.photos %>
<%= image_tag(url_for({:action => 'code_image',
:id => #article.photos.id})) -%>
<% end %>
articles/_formnew.html.erb
<% form_for (:article, #article,
:url => {:action=>'create'}, :html=>
{:multipart=>true}) do |f| %>
<%= f.label :title %><br />
<%= f.text_field :title %><br /><br />
<%= f.label :body %><br />
<%= f.text_area :body, :style => 'width: 600px;' %><br /><br />
<% f.fields_for :photos do |builder|%>
<%= builder.label :content, "Photo"%><br />
<%= builder.file_field :image_file %><br />
<% end %>
<br />
<%= f.submit "Create" %>
Thanks

Old question I know, but if somebody stumbles across it, this page helped me:
http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
Also, to display the image, you can just use:
image_tag [model].[paperclip_attachment].url
eg:image_tag trip_image.photo.url

Related

Carrierwave Rails with Edit Form and file_field (No file selected)

When my edit view loads, all my fields are populated with the record detail, but the file_field shows "No file Selected" next to the browse button. I would prefer that is show the record file name instead. Why does it not show that a file that was uploaded?
Edit View
<%= form_for #document, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :file, "File" %><br />
<%= f.file_field :file %><br />
<%= f.hidden_field :file_cache %><br />
<%= f.label :name, "name" %><br />
<%= f.text_field :name %><br />
<%= f.label :memo, "memo" %><br />
<%= f.text_area :memo %>
</p>
<%= f.hidden_field :folder_id %>
<p><%= f.submit "Upload" %></p>
<% end %>
I think a good counterquestion is "why should show an uploaded file by default?".
But basically is because security reasons and none of the browsers (yep, nothing to do with rails) is going to allow that, so you must to indicate manually to the user about the uploaded files.

rails form_for mongoid error

Hi i have a simple form for updating a user's account here is my controller
def edit
#users = User.find(params[:id])
end
here is my view
<%= form_for :user do |f| %>
<%= f.label :first_name %> <br />
<%= f.text_field :name ,:value => #users.name%><br />
<%= f.label :last_name %> <br />
<%= f.text_field :lname ,:value => #users.lname%><br />
<%= f.label :email %> <br />
<%= f.email_field :email ,:value => #users.email%><br />
<%= f.label :id %> <br />
<%= f.text_field :id ,:value => #users.id%><br />
<br />
<%= f.submit ({:confirm => "Are you sure?"}) %>
<% end %>
when i click submit i get the following mongoid error
Mongoid::Errors::InvalidFind in UsersController#edit
Problem:
Calling Document.find with nil is invalid.
Summary:
Document.find expects the parameters to be 1 or more ids, and will return a single document if 1 id is provided, otherwise an array of documents if multiple ids are provided.
Resolution:
Most likely this is caused by passing parameters directly through to the find, and the parameter either is not present or the key from which it is accessed is incorrect.
I'm fairly novice with rails and this is my first stab with mongoDB. Any input would help, Thank you.
Try to use this form
<%= form_for(#users) do |f| %>
...
...
<% end %>
The error comes because you are using :user that invalid find in your user controller

Paperclip gem don't shows pictures

I have successfully installed Paperclip gem and uploaded a picture to my post, but in show action there isn't any picture, just alt text with image alt.
I am using ruby on rails 3.2.1
#post form:
<% create_url = {:url=>{:action=>"create"}} if #post.new_record? %>
<% form_for #post, :html => { :multipart => true } do |t| %>
<b><%= t.label :title, 'Virsraksts:' %></b><br />
<%= t.text_field :title %><br /><br />
<b><%= t.label :content, 'Teksts:' %></b><br />
<%= t.text_area :content %><br /><br />
<%= f.file_field :bildes %>
<div class="actions">
<%= t.submit %>
</div>
#post show:
<p><%= #post.content %></p>
<%= image_tag #post.bildes.url %>
<%= image_tag #post.bildes.url(:medium) %>
<%= image_tag #post.bildes.url(:thumb) %>
In my opinion #post model url and path is not right, but exactly what I don't know.
#post model:
class Post < ActiveRecord::Base
has_attached_file :bildes, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:url => ":rails_root/app/assets/bildes/:id/:style/:basename.:extension",
:path => ":rails_root/app/assets/bildes/:id/:style/:basename.:extension"
end
Try using the default url/path for the attached file by not specifying it in the model.

Rails form multiple params XML

I have a form (snippet)
<% form_for(#transfer, :html => {:multipart => true}) do |f| %>
<p>
<%= f.label :source %><br />
<%= f.text_field :source %>
</p>
<p>
<%= f.label :destination %><br />
<%= f.text_field :destination %>
</p>
<% fields_for :upload do |u| %>
<p>
<%= u.label :upload %><br />
<%= u.text_field :upload %>
</p>
<% end %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', transfers_path %>
So now in my transfers controller I can do:
#transfer = Transfer.new(params[:transfer])
#upload = Upload.find_or_create_by_md5(params[:upload])
I am able to post to a single form with XML by simply changing the params to XML like
<transfer>
<source>foo</source>
<destination>bar</destination>
</transfer>
or
<upload>
<upload>baz</upload>
</upload>
But I cannot figure out how to combine them under the same XML root
Assuming you're using Rails 2.3.x, you might want to look into adding accepts_nested_attributes_for to your Transfer model. See what-s-new-in-edge-rails-nested-attributes.
Well I haven't been able to figure out how to do this for XML, so for now I've had to settle for doing it with REST. I came across the RestClient library and looking through the source, figured out you could do nested params like this:
RestClient.post( url,
{
:transfer => {
:path => '/foo/bar',
:owner => 'that_guy',
:group => 'those_guys'
},
:upload => {
:file => File.new(path)
}
})
Send a note to the author about documenting the functionality here

Order confirmation page in rails

I've been trying to create an order confirmation page for my rails app, and am not quite sure how to go about it in a restful way.
There were a few answers on this question that got me halfway there, but the problem was that I wasn't quite sure how to set up the form in the rails view so that it would take the user to a confirmation page with all their details instead of a create action.
Right now my view is simple:
<% form_for :order do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :first_name %><br />
<%= f.text_field :first_name, :size => 15 %>
</p>
<p>
<%= f.label :last_name %><br />
<%= f.text_field :last_name, :size => 15 %>
</p>
(Be sure to enter your name as it appears on your card)
<p>
<%= f.label :card_type %><br />
<%= f.select :card_type, [["Visa", "visa"], ["MasterCard", "master"], ["Discover", "discover"], ["American Express", "american_express"]] %>
</p>
<p>
<%= f.label :card_number %><br />
<%= f.text_field :card_number %>
</p>
<p>
<%= f.label :card_verification, "Card Verification Value (CVV)" %><br />
<%= f.text_field :card_verification, :size => 3 %>
</p>
<p>
<%= f.label :card_expires_on %><br />
<%= f.date_select :card_expires_on, :discard_day => true, :start_year => Date.today.year, :end_year => (Date.today.year+10), :add_month_numbers => true %>
</p>
<p><%= f.submit "Submit" %></p>
What things should I be doing to direct the user to a confirmation page that shows all the order details?
Thanks!
Kenji
There were a few answers on this
question that got me halfway there,
but the problem was that I wasn't
quite sure how to set up the form in
the rails view so that it would take
the user to a confirmation page with
all their details instead of a create
action.
Directing a form to a non standard page is pretty simple.
Add a url option form_for.
Such that
<% form_for :order do |f| %>
becomes
<% form_for :order :url => {:action => "confirm"} do |f| %>
You'll need to crate the confirm action in your routes, but that only involves this:
map.resources :orders, :collection => {:confirm => :get}
All you need now is a basic controller action and a view:
def confirm
#order = Order.new(params[:order])
unless #order.valid?
render :action => :new
else
end
end
Your view should look almost identical to the show view, with the addition of a form submitting #order to the create action.
Why don't you pull the confirmation via ajax for example, pull the result and put it as an overlay div, upon confirmation submit the original values in the form.
If you still need to do it your way then check wizardly, it's exactly designed for such uses.
I would like to update the answer for more elegant Rails 4 or up.
I hope it will help newbies like me. Ruby is awesome! :)
routes.rb
resources :orders do
collection do
post 'confirm'
end
end
orders_controller.rb
def confirm
#order = Order.new(order_params) # GET THE POST parameters
render :new if #order.invalid? # Return if false
end
form.html.erb
<%= form_for #order, url: {action: 'confirm'} do |f| %>

Resources