I''m still trying to get my head around Rails, so please excuse any overly dumb questions. That said, I could use a few pointers. First, I''m still trying to get my head around the relationship between controllers and view objects. Generating a scaffold for my controller throws in: def new @contact = Contact.new end What is @contact used for here, exactly. I guess I have an easier time understanding edit, list, and show because I can go into the views and find a @contact. But how is the new view using it? Is it involved with any of the form helper methods? Next question. I have a contacts table and a companies table. Contacts belong to a comany, which currently is just a single name field. I want to add a company field to contacts/new. Then in contacts/create I want to see if that company already exists and if it does, add the new contact to that company. If it doesn''t, I want to create the company and then add the contact through it. I think I can hands the updates to the view for this. Does ContactsController#new need to change? def new @contact = Contact.new @company = Company.new end That''s probably related to my first question. Then ContactsController#create is where I fall apart. Could I beg a hint or two of what that should look like? Thanks for your time. James Edward Gray II
> What is <at> contact used for here, exactly. I guess I have an easier time > understanding edit, list, and show because I can go into the views and > find a <at> contact. But how is the new view using it? Is it involved > with any of the form helper methods?@x is an instance variable x of the object @@x is a member variable of the class check out http://www.rubycentral.com/book/tut_classes.html jesse
* James Edward Gray II <james-AUi9nNu29NfWNcQ1/nO7itHuzzzSOjJt@public.gmane.org> [0341 21:41]:> I''m still trying to get my head around Rails, so please excuse any > overly dumb questions. That said, I could use a few pointers. > > First, I''m still trying to get my head around the relationship between > controllers and view objects. Generating a scaffold for my controller > throws in: > > def new > @contact = Contact.new > end > > What is @contact used for here, exactly. I guess I have an easier time > understanding edit, list, and show because I can go into the views and > find a @contact. But how is the new view using it? Is it involved > with any of the form helper methods?The other views are working on existing objects, typically retrieved by something like: def show @contact = Contact.find(....) end I''d guess you pass the new Contact instance to the view so the user can set attributes on it and save() it back. -- ''zzz..Kill all humans. Kill all humans..zzz ..... I was having the most wonderous dream. You were in it.'' -- Bender Rasputin :: Jack of All Trades - Master of Nuns
On Mar 24, 2005, at 3:56 PM, Jesse Andrews wrote:>> What is <at> contact used for here, exactly. I guess I have an >> easier time >> understanding edit, list, and show because I can go into the views and >> find a <at> contact. But how is the new view using it? Is it >> involved >> with any of the form helper methods? > > @x is an instance variable x of the object > @@x is a member variable of the class > > check out http://www.rubycentral.com/book/tut_classes.htmlThanks for the pointers. My Ruby is pretty solid though and I meant my questions to be more about the Rails aspect of how all this relates. James Edward Gray II
On Mar 24, 2005, at 4:21 PM, Dick Davies wrote:> The other views are working on existing objects, typically retrieved by > something like: > > def show > @contact = Contact.find(....) > end > > I''d guess you pass the new Contact instance to the view so the user can > set attributes on it and save() it back.Hmm, so @contact would automatically be filled when create() is called? Looks to me like the first line of scaffolding for create() fills it from the form info. James Edward Gray II
James Edward Gray II wrote:> I''m still trying to get my head around Rails, so please excuse any > overly dumb questions. That said, I could use a few pointers. > > First, I''m still trying to get my head around the relationship between > controllers and view objects. Generating a scaffold for my controller > throws in: > > def new > @contact = Contact.new > end > > What is @contact used for here, exactly.Instance variables are simply passed to the view. That''s all, no magic about that. In the view you can now do: Hello <%= @contact.name %>. Sascha
On Mar 24, 2005, at 7:23 PM, Sascha Ebach wrote:> James Edward Gray II wrote: >> I''m still trying to get my head around Rails, so please excuse any >> overly dumb questions. That said, I could use a few pointers. >> First, I''m still trying to get my head around the relationship >> between controllers and view objects. Generating a scaffold for my >> controller throws in: >> def new >> @contact = Contact.new >> end >> What is @contact used for here, exactly. > > Instance variables are simply passed to the view. That''s all, no magic > about that. In the view you can now do: > > Hello <%= @contact.name %>.Of course, there wouldn''t be a name for a newly created blank Contact. ;) Still, what you said helps. I believe I''m beginning to get it. I''m pretty sure @contact is being used in the construct <%= error_messages_for ''contact'' %>. I also figured out how to set my Company as needed. Turned out to be very easy. I''m getting there... Thanks again for all the help. James Edward Gray II
On Thu, Mar 24, 2005 at 07:51:04PM -0600, James Edward Gray II wrote:> On Mar 24, 2005, at 7:23 PM, Sascha Ebach wrote: > >Hello <%= @contact.name %>. > > Of course, there wouldn''t be a name for a newly created blank Contact.Not the best example, perhaps, but the idea is right on. When the object instance is created, its attributes will be populated with DB defaults pulled through ActiveRecord. If you use the form helpers, the user will be presented with a form that has default values filled in, where appropriate. If you don''t use form helpers, you can access the defaults through the object reference. If you don''t want default values at all (or your DB schema doesn''t set them), then you don''t need the instantiation before the view rendering. Andrew