Je n'arrive pas à le faire fonctionner pour mon cas. Je m'explique. J'ai 2 modèles: Grant et Grantrequest. Je dois associer un ou plusieurs GrantsRequests à un Grant.
J'ai un modèle Grant:
[code
class Grant < ActiveRecord::Base
has_many :grant_requests, :dependent => :destroy
belongs_to :grant_scheme
belongs_to :dept_involved, :class_name => "DepartmentInvolved" , :foreign_key => :dept_involved
belongs_to :external_partner, :class_name => "ExternalPartner" , :foreign_key => :grant_ext_partner
end
class DepartmentInvolved < Grant
has_many :grants
end
class ExternalPartner < Grant
has_many :grants
end
[/code]
et modèle de GrantRequest:
Dans ma page grant/new j'ai:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 class GrantRequest < ActiveRecord::Base belongs_to :grant has_one :db_file, :dependent => :destroy has_attachment :max_size => 1.megabyte validates_as_attachment end
Ne faites pas atention à <%= submit_button("Create") %>, j'utilise silky-button plugin piour avoir de jolis boutons.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <h4>New grant</h4> <% form_for(@grant, { :multipart => true }) do |f| %> <%= f.error_messages %> <%= render :partial => 'grant'%> <p> <%= submit_button("Create") %> </p> <% end %> <%= link_to 'Back', grants_path %>
Dans mon GrantsController:
Après l'exécution j'ai l'erreur suivante:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 def new @grant = Grant.new @grant_requests = GrantRequest.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @grant } end end def create @grant = Grant.new(params[:grant]) @grant.grant_requests << GrantRequest.new(params[:grant_request]) respond_to do |format| if @grant.save flash[:notice] = 'Grant was successfully created.' format.html { redirect_to(:action=>:index) } format.xml { render :xml => @grant, :status => :created, :location => @grant } else format.html { render :action => "new" } format.xml { render :xml => @grant.errors, :status => :unprocessable_entity } end end end
Dans README l'auteur dit que content_type par defaut accepte tous les types de fichiers, en réalité (si je ne me trompe pas) dans le code (voir module ClassMethods dans attachement_fu.rb, ligne 125 du plugin):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 NoMethodError in GrantsController#create undefined method `content_type' for "test_file.txt":String vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb:294:in `uploaded_data='
Alors content_type est obligatoire?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 validates_presence_of :size, :content_type, :filename
Même si je rajoute dans mon model
ça ne change rien, l'erreur est la même.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2 :content_type => ['application/pdf', 'application/msword', 'text/plain']
Quelqu'un peut m'expliquer où j'ai eu tort.
Précision: je suis sous Windows XP, Rails 2.2.2 gem version 1.3.1. Merci
Partager