Forum: Ruby on Rails Basic MVC in RoR question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Basic MVC in RoR question (Guest)
on 2006-02-06 04:22
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?
Joe Van D. (Guest)
on 2006-02-06 04:57
(Received via mailing list)
On 2/5/06, Basic MVC in RoR question <removed_email_address@domain.invalid> 
wrote:
>
> My View is:
>
> 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
Basic MVC in RoR question (Guest)
on 2006-02-06 05:20
> 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.
Byron S. (Guest)
on 2006-02-06 05:58
(Received via mailing list)
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
Brian V. Hughes (Guest)
on 2006-02-06 16:26
(Received via mailing list)
Basic MVC in RoR question wrote:
>
> 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
> end

Also, 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])
> end


And 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
This topic is locked and can not be replied to.