ruby on rails - Using paperclip gem to add an attachment -


i've attempted use paperclip gem add attachments contact model. looks though can add attachment - can select file without problem. when create contact, attachment not saved. know because can see in rails console document_id 'nil'.

in _form.html.erb creating new contact, have:

<%= simple_form_for(@contact, html: {class: "form-inline well", multipart: true}, role: "form") |f| %>         <% if @contact.errors.any? %>           <div id="error_explanation">             <h2><%= pluralize(@contact.errors.count, "error") %> prohibited contact being saved:</h2>              <ul>             <% @contact.errors.full_messages.each |msg| %>               <li><%= msg %></li>             <% end %>             </ul>           </div>         <% end %>          <div class="form-group">            <%= f.input :name %>           <%= f.input :category %>           <%= f.input :area %>           <%= f.input :phone %>           <%= f.input :website %>           <%= f.input :email %>            <%= f.fields_for :document |document_fields| %>             <%= document_fields.input :attachment, as: :file %>           <% end %>            <%= f.button :submit, class: "btn-success" %>         </div>       <% end %> 

my db migration was:

class createdocuments < activerecord::migration   def change     create_table :documents |t|          t.integer :user_id         t.timestamps     end      add_index :documents, :user_id     add_attachment :documents, :add_attachment     add_column :contacts, :document_id, :integer   end end 

my model document.rb is:

class document < activerecord::base      has_attached_file :attachment, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"     validates_attachment_content_type :attachment, :content_type => /\aimage\/.*\z/ end 

and contact.rb is:

class contact < activerecord::base     belongs_to :user     belongs_to :document      accepts_nested_attributes_for :document      validates :name, presence: true,                         length: { minimum: 2}      validates :user_id, presence: true end 

and actions in contacts_controller.rb are:

    def create     @contact = current_user.contacts.new(contact_params)      respond_to |format|       if @contact.save         format.html { redirect_to @contact, notice: 'contact created.' }         format.json { render action: 'show', status: :created, location: @contact }       else         format.html { render action: 'new' }         format.json { render json: @contact.errors, status: :unprocessable_entity }       end     end   end   def contact_params     params.require(:contact).permit(:name, :email, :category, :area, :organisation, :website, :phone, :user_id, document_attributes: [:id, :attachment])    end    # patch/put /contacts/1   # patch/put /contacts/1.json   def update      @contact = current_user.contacts.find(params[:id])     if params[:contact] && params[:contact].has_key?(:user_id)       params[:contact].delete(:user_id)      end     respond_to |format|       if @contact.update((contact_params))         format.html { redirect_to @contact, notice: 'contact updated.' }         format.json { head :no_content }       else         format.html { render action: 'edit' }         format.json { render json: @contact.errors, status: :unprocessable_entity }       end     end    end 

when try create new contact attachment, here heroku log:

2014-03-10t23:41:49.594786+00:00 app[web.1]: parameters: {"utf8"=>"✓", "authenticity_token"=>"fgitlgkhzfw15ykscusgdvwdb//cgmyomfh4ss4l63y=", "contact"=>{"name"=>"oliver ot", "category"=>"ot", "area"=>"rockdale", "phone"=>"", "website"=>"", "email"=>"", "document_attributes"=>{"attachment"=>#, @original_filename="screen shot 2014-03-03 @ 9.24.40 pm.png", @content_type="image/png", @headers="content-disposition: form-data; name=\"contact[document_attributes][attachment]\"; filename=\"screen shot 2014-03-03 @ 9.24.40 pm.png\"\r\ncontent-type: image/png\r\n">}}, "commit"=>"create contact"} 2014-03-10t23:41:49.594786+00:00 app[web.1]: parameters: {"utf8"=>"✓", "authenticity_token"=>"fgitlgkhzfw15ykscusgdvwdb//cgmyomfh4ss4l63y=", "contact"=>{"name"=>"oliver ot", "category"=>"ot", "area"=>"rockdale", "phone"=>"", "website"=>"", "email"=>"", "document_attributes"=>{"attachment"=>#, @original_filename="screen shot 2014-03-03 @ 9.24.40 pm.png", @content_type="image/png", @headers="content-disposition: form-data; name=\"contact[document_attributes][attachment]\"; filename=\"screen shot 2014-03-03 @ 9.24.40 pm.png\"\r\ncontent-type: image/png\r\n">}}, "commit"=>"create contact"}

i'm not sure how attachment save correctly?

move contact_params method outside of update action can accessed create action , change following code.

def contact_params   params.require(:contact).permit(:name, :email, :category, :area, :organisation, :website, :phone, :user_id, document_attributes: [:id, :attachment]) end 

the code above allows id , attachment params document_attributes.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -