Jseb
Jseb

Reputation: 1934

Jquery basic file upload

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??

Upvotes: 2

Views: 469

Answers (2)

Sarah A
Sarah A

Reputation: 1233

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

Upvotes: 1

RadBrad
RadBrad

Reputation: 7304

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.

Upvotes: 0

Related Questions