2

I been working on a screencast pro proposed by Ryan called the Jquery file uploads. However every time i input images it doesn't get uploaded. I am not sure what i am doing wrong here my code. I am using google and did try to upload it but it doesn't seem to be doing anything

Javascript application.js

//= require jquery
//= require jquery_ujs
//= require jquery-fileupload/basic
//= require jquery-fileupload/vendor/tmpl
//= require_tree .

painting.js.coffee

jQuery ->
  $('#new_painting').fileupload

Views _form.html.erb

<%= form_for @painting, :html => {:multipart => true} do |f| %>
  <div class="field">
    <%= f.hidden_field :galley_id %>
  </div>
  <div class="field">
    <%= f.label :image %><br />
    <%= f.file_field :image %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

index.html.erb

Painting Gallery

<%= form_for Painting.new do |f| %>
  <%= f.label :image, "Upload paintings:" %>
  <%= f.file_field :image, multiple: true, name: "painting[image]" %>
<% end %>

show.html.erb

<p><%= image_tag @painting.image_url %></p>

<p>
  <%= link_to "Edit", edit_painting_path(@painting) %> |
  <%= link_to "Destroy", @painting, :confirm => 'Are you sure?', :method => :delete %> |
  <%= link_to "Back to Paintings", paintings_path %>
</p>

Controller

def index
    @paintings = Painting.all
  end

  def show
    @painting = Painting.find(params[:id])
  end

  def new
    @painting = Painting.new
  end

  def create
    @painting = Painting.create(params[:painting])
  end

  def edit
    @painting = Painting.find(params[:id])
  end

  def update
    @painting = Painting.find(params[:id])
    if @painting.update_attributes(params[:painting])
      redirect_to paintings_url, notice: "Painting was successfully updated."
    else
      render :edit
    end
  end

  def destroy
    @painting = Painting.find(params[:id])
    @painting.destroy
    redirect_to paintings_url, notice: "Painting was successfully destroyed."
  end

Model

  attr_accessible :image
  mount_uploader :image, ImageUploader

My main issues is when it comes to upload any picture it just doesn't have a submit button and not sure how to make it work, did i miss something??

2 Answers 2

1

So just got this fixed by adding timezone (i.e utc) to the expiration in S3Uploader > initialize method

for more info see: upload to amazon S3

Sign up to request clarification or add additional context in comments.

Comments

0

You are likely successfully uploading the file to your controller action, but you're not doing anythin with it other than

@painting = Painting.create(params[:painting])

You need to do more, like maybe saving the uploaded file somewhere on the server?

def  create
  filename = params[:painting][:image].original_filename
  filetype = params[:painting][:image].content_type
  filedata = params[:painting][:image].read
  File.open("#{Rails.root.to_s}/images/#{filename}","wb") { |f| f.write(filedata) } 
  @painting = Painting.create(params[:painting])
end

This will extract the uploaded image and write it to the server. Content_type is handy if you want to record the MIME type in the database as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.