Topic: Attachment_fu/Create form Code Check Please

Hey guys,

Just managed to sort out the attachment_fu plugin. I have got to the stage where i can choose a file to upload but nothing is actually being uploaded or saved to the databse when i hit the Create button. Would someone take a quick look at my code for me please to check i am on the right path? Cheers

Controller...

  def new
    @collector = Collector.new
  end

  def create
    @collector = Collector.new(params[:collector])
    if @collector.save
      flash[:notice] = 'Collector was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end


Model...

class Collector < ActiveRecord::Base

  has_attachment  :content_type => :image,
                           :processor => :mini_magick,
                           :storage => :file_system,
                           :max_size => 500.kilobytes,
                           :resize_to => '320x200>',
                           :thumbnails => { :thumb => '100x100>' },
                           :path_prefix => "/public/uploaded_files"
  validates_as_attachment
 
end


New Record view...

<%= error_messages_for :controller %>

<%= form_tag( { :action => 'create' }, :multipart => true ) %>
     
   <%= render :partial => 'create' %>
   
   <%= render :partial => 'photo' %>
   </p>
  <p>
    <%= submit_tag 'Create' %>
  </p>
  <% end %>


Create Partial...

<%= error_messages_for 'collector' %>

<!--[form:collector]-->
 
<p><label for="collector_FIRST_NAME">First name</label><br/>
<%= text_field 'collector', 'FIRST_NAME'  %></p>

<p><label for="collector_SECOND_NAME">Second name</label><br/>
<%= text_field 'collector', 'SECOND_NAME'  %></p>

<p><label for="collector_DOB">Dob</label><br/>
<%= date_select 'collector', 'DOB'  %></p>

<p><label for="collector_EMAIL">Email</label><br/>
<%= text_field 'collector', 'EMAIL'  %></p>

<p><label for="collector_WEBSITE">Website</label><br/>
<%= text_field 'collector', 'WEBSITE'  %></p>

<!--[eoform:collector]-->


Photo Partial...

<% form_for(:collector, :url => collectors_path, :html => { :multipart => true }) do |f| -%>
  <p>
  <label for="Photo">Upload A Photo:</label>
    <%= f.file_field :uploaded_data %>
    </p>
  <p>
  <% end -%>

Think thats everything...oh and the error message i am getting is...

SyntaxError in Collectors#new

Showing app/views/collectors/new.rhtml where line #13 raised:

compile error
./script/../config/../app/views/collectors/new.rhtml:13: parse error, unexpected kEND, expecting $
Extracted source (around line #13):

10:     <%= submit_tag 'Create' %>
11:   </p>
12:   <% end %>


Thanks in advance for any help anyone can offer.

Pete

Re: Attachment_fu/Create form Code Check Please

form_tag() doesn't need an <% end %>, it needs a pur-HTML </form>

Re: Attachment_fu/Create form Code Check Please

Ah ok cool that works, however my Create button on the New form does absolutely nothing??

Thanks

Re: Attachment_fu/Create form Code Check Please

what do you mean by "does absolutely nothing?

What i can see here:
You render a form for the photo inside another form. That's invalid HTML and not gonna work.
why do you do this? you could simply add the data field to the create partial ...

try this:

<%= error_messages_for 'collector' %>

<!--[form:collector]-->
 
<p><label for="collector_FIRST_NAME">First name</label><br/>
<%= text_field 'collector', 'FIRST_NAME'  %></p>

<p><label for="collector_SECOND_NAME">Second name</label><br/>
<%= text_field 'collector', 'SECOND_NAME'  %></p>

<p><label for="collector_DOB">Dob</label><br/>
<%= date_select 'collector', 'DOB'  %></p>

<p><label for="collector_EMAIL">Email</label><br/>
<%= text_field 'collector', 'EMAIL'  %></p>

<p><label for="collector_WEBSITE">Website</label><br/>
<%= text_field 'collector', 'WEBSITE'  %></p>

<p><label for="Photo">Upload A Photo:</label>
    <%= file_field 'collector','uploaded_data' %></p>

<!--[eoform:collector]-->


Though i'm not sure if thats the main problem.

Re: Attachment_fu/Create form Code Check Please

Ok not sure what i was thinking back there, i have just tried your suggestion and it works brilliantly thank you.

What i meant about the Create button was that it was not doing anything when i clicked it, the system was not trying to process the form or even throw back an error.

Anyway the code i have now which is working is as follows...

Controller...

 def new
    @collector = Collector.new
  end

  def create
    @collector = Collector.new(params[:collector])
    if @collector.save
      flash[:notice] = 'Collector was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end


Model...
  has_attachment  :content_type => :image, 
                           :processor => :mini_magick,
                           :storage => :file_system,
                           :max_size => 500.kilobytes,
                           :resize_to => '320x200>',
                           :thumbnails => { :thumb => '100x100>' }
                           
  validates_as_attachment

New Record View...

<%= error_messages_for :collector %>

<%= form_tag( { :action => 'create' }, :multipart => true ) %>
     
   <%= render :partial => 'create' %>
   </p>
  <p>
    <%= submit_tag 'Create' %>


Create Partial...

<%= error_messages_for 'collector' %>

<!--[form:collector]-->
 
<p><label for="collector_FIRST_NAME">First name</label><br/>
<%= text_field 'collector', 'FIRST_NAME'  %></p>

<p><label for="collector_SECOND_NAME">Second name</label><br/>
<%= text_field 'collector', 'SECOND_NAME'  %></p>

<p><label for="collector_DOB">Dob</label><br/>
<%= date_select 'collector', 'DOB'  %></p>

<p><label for="collector_EMAIL">Email</label><br/>
<%= text_field 'collector', 'EMAIL'  %></p>

<p><label for="collector_WEBSITE">Website</label><br/>
<%= text_field 'collector', 'WEBSITE'  %></p>

<p><label for="Photo">Upload A Photo:</label>
    <%= file_field 'collector','uploaded_data' %></p>

<!--[eoform:collector]-->


The only concern i have now is that the image is not being displayed in the  List view. However, the link to the file from the list view, successfully displays the image in the seperate page, indicating that the list view is producing the correct path to the file. Within the list view the image is displayed as a question mark in a blue box.

The call im making is...

<%= image_tag(collector.public_filename(:thumb)), collector.public_filename %><br/>

Cheers thank you.


PS. Anybody having problems posting code to this forum from Safari for Windows?