anton87 — 2011-06-27T14:01:15-04:00 — #1
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,
parkint — 2011-06-28T08:14:45-04:00 — #2
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.
anton87 — 2011-06-28T09:00:39-04:00 — #3
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?
bhawk90 — 2011-07-08T03:31:38-04:00 — #4
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| %>
<%= f.text_field :name %>
<%= f.text_field :link %>
<%= submit_tag %>
<% end %>
anton87 — 2011-07-08T09:56:37-04:00 — #5
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.
bhawk90 — 2011-07-08T14:28:13-04:00 — #6
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.
anton87 — 2011-07-08T15:58:31-04:00 — #7
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.
bhawk90 — 2011-07-08T23:47:01-04:00 — #8
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.
anton87 — 2011-07-12T21:52:44-04:00 — #9
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.