vendredi 8 mai 2015

add info from rails_admin

i'm new in rails, and i'm building a rails app for a architect, so a client have many pictures. So i make my DB client and picture, and their link are in picture, the client_id.

https://www.youtube.com/watch?v=abcnfFS_DS8 This video help me a lot (see around 30min)

I don't want to create some forms or scaffolding system, i'd like to use the rails_admin gem to upload my picture(this is ok) and i propose to makes the relation and choose witch client i'd like to use, but this let my client_id at nil.

What should i do ?

This is my controller :

class RealisationController < ApplicationController
def real
    @clients = Client.all
    @pics = Picture.all
end

This is my models :

class Picture < ActiveRecord::Base
    belongs_to :client
    attr_accessible :client_name, :picture, :client, :image

    has_attached_file :image,
        :styles => { :medium => "300x300", :thumb => "100x100>" },
        :path => ":rails_root/public/images/:id/:filename", #a changer pour mettre :id_client
        :url  => "/images/:id/:filename"

    validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

end
class Client < ActiveRecord::Base
    # validates_presence_of :client_name
    attr_accessible :client_name, :picture, :description
    has_many :pictures, dependent: :destroy
    accepts_nested_attributes_for :pictures, :allow_destroy => true
    attr_accessible :pictures_attributes
end

This are my DB creation :

        class CreatePictures < ActiveRecord::Migration
      def change
        create_table :pictures do |t|
          t.integer  :client_id
          t.timestamps null: false
        end
      end
    end

class AddAttachmentImageToPictures < ActiveRecord::Migration
  def self.up
    change_table :pictures do |t|
      t.attachment :image
    end
  end

  def self.down
    remove_attachment :pictures, :image
  end
end

class CreateClients < ActiveRecord::Migration
  def change
    create_table :clients do |t|
        t.string   :client_name
        t.string   :description
      t.timestamps null: false
    end
  end
end

I feel it ! i'm really close to my goal ! But some help could be really helpful ..

Thanks for reading me, and sorry for my grammatical mistakes, i'm french ;)

Aucun commentaire:

Enregistrer un commentaire