Hi everybody, These are my first steps using RoR. I''m trying to modify the code created by scaffold. So the methods new and create in the controller look like this: def new @myfile = Myfile.new id = @params[:id] if id == nil id = 0 end @myfile.folder_id = id #save the new file in the correct spot end def create @myfile = Myfile.new(params[:myfile]) if @myfile.save flash[''notice''] = ''Myfile was successfully created.'' redirect_to :controller => ''folder'', :action => ''list'' else render_action ''new'' end end In the "new" view I''m storing the folder_id in a hidden form field like this: <%= hidden_field ''folder'', ''folder_id'' %> - Is this the right way or can/should this be solved without using the hidden form feild?? - Also, after saving how can I redirect to the id in the hidden form field? - Last question: I''m using folder_id (this is the name of the field in the database and it works fine), but @myfile.folder is actually referencing the folder object. However when I try to store a folder with Myfile like this: @myfile.folder = @params[:folder] I get an error message. Is using folder_id a good idea?? Thanks! Grt, Mischa.
Mischa Berger wrote:> - Is this the right way or can/should this be solved without using the > hidden form feild??You could just pass it in the parameters to your action when you start your form tag (via form helper).> - Also, after saving how can I redirect to the id in the hidden form field?Just use redirect_to, with parameters (action, id, etc.) to get it where you want.> - Last question: I''m using folder_id (this is the name of the field in > the database and it works fine), but @myfile.folder is actually > referencing the folder object. However when I try to store a folder with > Myfile like this: @myfile.folder = @params[:folder] I get an error > message. Is using folder_id a good idea??I''m confused on this one.. The Rails idiom is to have "id" as a primary key, and something "folder_id" for references into a table named "folders". --Steve
Thanks, for your help.>>- Last question: I''m using folder_id (this is the name of the field in >>the database and it works fine), but @myfile.folder is actually >>referencing the folder object. However when I try to store a folder with >>Myfile like this: @myfile.folder = @params[:folder] I get an error >>message. Is using folder_id a good idea?? > > > I''m confused on this one.. The Rails idiom is to have "id" as a primary > key, and something "folder_id" for references into a table named "folders".Yes I''m using folder_id to reference a table named folders, but it seems using both @myfile.folder_id = 1 and myfile.folder = Folder.find(1) work fine. I guess the second way of storing a myfile in a folder is the preferred way? Grt, Mischa.
> Yes I''m using folder_id to reference a table named folders, but it > seems using both @myfile.folder_id = 1 and myfile.folder = > Folder.find(1) work fine. I guess the second way of storing a > myfile in a folder is the preferred way?1) @myfile.folder_id = 1 2) @myfile.folder = Folder.find(1) Yeah, go with the second one. It''s more stylish :) And yeah, I guess it''s the preferred way Are you trying to upload a file? Oh, and a small tip. You can rewrite this: if id == nil id = 0 end into this id = 0 if id == nil That''s an "if modifier" in Ruby cheers, rob
The first saves you a query though :-) Jaap On 10/22/05, Mischa Berger <spmm_pls-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> Thanks, for your help. > > >>- Last question: I''m using folder_id (this is the name of the field in > >>the database and it works fine), but @myfile.folder is actually > >>referencing the folder object. However when I try to store a folder with > >>Myfile like this: @myfile.folder = @params[:folder] I get an error > >>message. Is using folder_id a good idea?? > > > > > > I''m confused on this one.. The Rails idiom is to have "id" as a primary > > key, and something "folder_id" for references into a table named "folders". > Yes I''m using folder_id to reference a table named folders, but it seems > using both @myfile.folder_id = 1 and myfile.folder = Folder.find(1) work > fine. I guess the second way of storing a myfile in a folder is the > preferred way? > > Grt, > Mischa. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> Are you trying to upload a file?Yes, to the database.> Oh, and a small tip. You can rewrite this: > > if id == nil > id = 0 > end > > into this > > id = 0 if id == nil > > That''s an "if modifier" in RubyThanks for the tip! Grt, Mischa.
On Oct 22, 2005, at 3:07 PM, Mischa Berger wrote:>> Are you trying to upload a file? >> > Yes, to the database. > > >> Oh, and a small tip. You can rewrite this: >> if id == nil >> id = 0 >> end >> into this >> id = 0 if id == nil >> That''s an "if modifier" in Ruby >> > Thanks for the tip! > > Grt, > Mischa. >You can also write this as: id.nil? || 0 And then if id is nil it will be set to 0 if it is not nil it will be left alone. Cheers- -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra-gdxLOakOTQ9oetBuM9ipNAC/G2K4zDHf@public.gmane.org 509-577-7732 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> You can also write this as: > > id.nil? || 0 > > And then if id is nil it will be set to 0 if it is not nil it will be > left alone. >That doesn''t work for me: irb(main):002:0> id = nil => nil irb(main):003:0> id.nil? => true irb(main):004:0> id.nil? || 0 => true irb(main):005:0> id => nil this alternative does work: irb(main):008:0> id.nil? && id=0 => 0 irb(main):009:0> id => 0 irb(main):010:0> id = 4 => 4 irb(main):011:0> id.nil? && id=0 => false irb(main):012:0> id => 4 and this: irb(main):014:0> id = nil => nil irb(main):015:0> !id.nil? || id = 0 => 0 irb(main):016:0> id => 0 irb(main):017:0> id = 4 => 4 irb(main):018:0> !id.nil? || id = 0 => true irb(main):019:0> id => 4 tho i''m not sure whether these are as clear as the original suggestion of id = 0 if id == nil Greg
> id = 0 if id == nilSome other alternatives: id = 0 if id.nil? Or id = 0 unless id Or id ||= 0 -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Mischa Berger Sent: Sunday, 23 October 2005 8:07 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: [Rails] Re: What is the "right" way to program this?> Are you trying to upload a file?Yes, to the database.> Oh, and a small tip. You can rewrite this: > > if id == nil > id = 0 > end > > into this > > id = 0 if id == nil > > That''s an "if modifier" in RubyThanks for the tip! Grt, Mischa. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Perhaps this is what you mean: id ||= 0> > You can also write this as: > > id.nil? || 0 > > And then if id is nil it will be set to 0 if it is not nil it will be > left alone. >irb(main):020:0> id=nil => nil irb(main):021:0> id ||= 0 => 0 irb(main):022:0> id => 0