Ok, Here''s my controller: def create trainingplan = Trainingplan.createplan(@params[:startdate], @params[:enddate], @params[:hoursperweek], @params[:programtype]) end My model is: class Trainingplan <ActiveRecord::Base def self.createplan(stardate, enddate, hoursperweek, programtype) stardate = startdate enddate = enddate hoursperweek = hoursperweek programtype = programtype end end My View is: <%= @trainingplan.startdate %> What I''m trying to do is add "annualhours" which is just hoursperweek*52. So I add to my model: annualhours = hoursperweek*52 However, this gives me a nil object error. I know this is simple and I''ve read the Agile book but still I''m not getting it. What exactly do I need to add to my controller or model and view in order to calculate and display the annualhours? -- Posted via http://www.ruby-forum.com/.
On 2/5/06, Basic MVC in RoR question <webslinger143@hotmail.com> wrote:> Ok, Here''s my controller: > > def create > trainingplan = Trainingplan.createplan(@params[:startdate], > @params[:enddate], @params[:hoursperweek], @params[:programtype]) > > end > > > My model is: > > class Trainingplan <ActiveRecord::Base > def self.createplan(stardate, enddate, hoursperweek, programtype) > stardate = startdate > enddate = enddate > hoursperweek = hoursperweek > programtype = programtype > end > end > > > My View is: > > <%= @trainingplan.startdate %> > > > > > What I''m trying to do is add "annualhours" which is just > hoursperweek*52. > > So I add to my model: > > annualhours = hoursperweek*52 > > However, this gives me a nil object error. > > I know this is simple and I''ve read the Agile book but still I''m not > getting it. > > What exactly do I need to add to my controller or model and view in > order to calculate and display the annualhours?Er, annualhours is probably a local variable in the model, so it goes away at the end of the method call. That''s why you don''t see it in the view. What you want is a new method called annualhours (although it should be called annual_hours). So: class TrainingPlan < AR def annual_hours self.hoursperweek * 52 end end
> Er, annualhours is probably a local variable in the model, so it goes > away at the end of the method call. That''s why you don''t see it in > the view. > > What you want is a new method called annualhours (although it should > be called annual_hours). > > So: > > class TrainingPlan < AR > def annual_hours > self.hoursperweek * 52 > end > endannualhours is part of the model as it is a column in the database. Therefore I assumed it would be part of Trainingplan. I don''t understand why it would be local to the model and not available in the view? I was under the impression that instance variables like @annualhours were local to the model, controller, or view and variables that were part of the model were accessible in the view. So if I have annualhours defined as a part of the trainingplan table, shouldn''t I be able in the model to do: annualhours = hoursperweek*52 and in the view do: @trainingplan.annualhours This however doesn''t work. -- Posted via http://www.ruby-forum.com/.
Can you post more complete code from the controller, model, and view? It sounds like it should work more or less, so it could be some subtle problem in your code. I''ve noticed, for instance, a few typos above and I''m not sure if that is your actual code or you re-typed it. - byron On 2/5/06, Basic MVC in RoR question <webslinger143@hotmail.com> wrote:> > > Er, annualhours is probably a local variable in the model, so it goes > > away at the end of the method call. That''s why you don''t see it in > > the view. > > > > What you want is a new method called annualhours (although it should > > be called annual_hours). > > > > So: > > > > class TrainingPlan < AR > > def annual_hours > > self.hoursperweek * 52 > > end > > end > > annualhours is part of the model as it is a column in the database. > Therefore I assumed it would be part of Trainingplan. I don''t > understand why it would be local to the model and not available in the > view? I was under the impression that instance variables like > @annualhours were local to the model, controller, or view and variables > that were part of the model were accessible in the view. So if I have > annualhours defined as a part of the trainingplan table, shouldn''t I be > able in the model to do: > > annualhours = hoursperweek*52 > > and in the view do: > > @trainingplan.annualhours > > This however doesn''t work. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Byron http://byron.saltysiak.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060206/c82725e4/attachment.html
Basic MVC in RoR question wrote:> annualhours is part of the model as it is a column in the database. > Therefore I assumed it would be part of Trainingplan. I don''t > understand why it would be local to the model and not available in the > view? I was under the impression that instance variables like > @annualhours were local to the model, controller, or view and variables > that were part of the model were accessible in the view. So if I have > annualhours defined as a part of the trainingplan table, shouldn''t I be > able in the model to do: > > annualhours = hoursperweek*52 > > and in the view do: > > @trainingplan.annualhours > > This however doesn''t work.If annualhours is a column in the table, I believe you need to do self.annualhours within the model class in order to set or access its value.> class Trainingplan <ActiveRecord::Base > def self.createplan(stardate, enddate, hoursperweek, programtype) > stardate = startdate > enddate = enddate > hoursperweek = hoursperweek > programtype = programtype > end > endAlso, what you''re doing here isn''t actually creating a row in the database. At no time do you create a new TrainingPlan class, and you don''t make a call to that objects save method. So I think you need to go back to your controller:> def create > trainingplan = Trainingplan.createplan(@params[:startdate], > @params[:enddate], @params[:hoursperweek], @params[:programtype]) > endAnd change this code to something more like this: def create trainingplan = TrainingPlan.create ( :stardate => @params[:startdate], :enddate => @params[:enddate], :hoursperweek => @params[:hoursperweek], :annualhours => @params[:hoursperweek].to_i * 52, :programtype => @params[:programtype]) end -Brian