Why is it necessary to have two instantiations of the Story.new object?


I’m going through the Shovell tutorial in Simply Rails 2 and have a question. By page 166 of the book the StoriesController class has two methods with instantiations of Story.new objects. The two methods are the ‘new’ and ‘create’ methods. Both methods instantiate the Story.new object and then assign it to the @story instance variable. I think I am looking at this correctly. Why is that? Are these both local variables to their respective methods or are they the same? I’m finally gaining some good traction as to the relationship between model-view-controller, all the files associated and the resource routing but this is really perplexing me at this point. Can someone please help?

Thanks a lot,


Those methods instantiate a new object of type Story.
Remember that Story is the class and @story is an instance variable of that class.
The instance variable (prefixed with @) is global in scope.

Okay but why would you do this sequence ‘@story=Story.new’ two times? As far as I can tell it’s done once in the ‘new’ method to create an action that will populate the @story instance variable but then the same sequence is done in the ‘create’ method. Isn’t the instance variable @story of the Story class already instantiated or is there another purpose?


I believe instantiating a Story object and assigning it to the @story instance variable gives the form_for method in the new.html.erb view something to work with, since it takes an object as a parameter before the block in which you make the form:

<% form_for @story do |f| %>
  name:<br />
  <%= f.text_field :name %>
  link:<br />
  <%= f.text_field :link %>
  <%= submit_tag %>
<% end %>

Now I see it. Thank you. I was not reading the explanation for both methods carefully enough. The ‘new’ action creates the @story instance variable and then the ‘create’ action actually populates it upon the user hitting Submit. Thanks again.

No problem, I just finished that part of the book too. Seems quite a bit easier than PHP so far, just hope Rails 3 isn’t too much different.

No kidding? I’m pretty new to web development (but not to programming) and I just took a class that covered client-side/server-side VBScript, JScript, ASP and then PHP. Admittedly the class was an introduction but PHP seemed less convoluted than RoR to me. The whole MVC approach and the myriad of files are a little overwhelming. I’ve been through the book three times up to Chapter 7 and I think I finally am starting to pull it all together but there are still a few things I’m not real clear on. I think it’s going to take working through the Shovell example and then develop something of my own before I will have a “clear picture”. Anyway Thanks for the help.

I’ve been doing both web development and programming for several years, and I don’t think I’d recommend RoR to someone without a whole lot of experience in web development just because of all the concepts and the “myriad of files”. However, once you’ve been programming in another language like PHP for a while, especially without the use of a framework, RoR makes a lot of sense. Using MVC makes it a lot more manageable, and that pattern isn’t unique to web development, it can be applied to other types of programming as well. I’m enjoying going through the book, it makes building an application a lot less work. It helps to have a good background in web development though, to understand what Rails is doing for you.

Cool, I appreciate the insight. The book doesn’t assume too much but even considering that I can see where some details are left out. I’ve read the book all the way through Chapter 7 three times. The first time I just read the material and I must say I was not getting a lot of it. The second time I did the “development” right along with the book but by chapter 7 I realized I was quite confused about what all files were being generated by the generate script(s) for instance. This time I have been writing each and every file down in a notebook along with explanations of the helpers, partials, templates, RJS files, relationships between controller actions and views, etc. I actually understand the majority of what’s going on but at the same time I wonder if I will be able to transfer the knowledge to a website of my own design. Thanks again for the insight. I am definitely a novice at web development so maybe I should continue working with PHP for awhile. RoR is quite interesting though.