Okay, so this is really a beginner''s question. I''m just starting to move over from ColdFusion to Rails and I''m having some trouble accessing ActiveRecord objects from within views. I''m sure I''m missing something pretty obvious. I have a page model, a pages_controller, a content model and a content_controller. The idea is for the content controller to manage the administrator''s interaction with the content and for pages to display the content to visitors. But I''m having problems referencing the variables inside the pages_controller. I have: def show @page=Page.find(params[:id]) @content = Content.find(:all) end I have a partial view _header.rhtml that just has: <% for page in @pages %> <ul> <li><%=@page.title%></li> </ul> <%end%> There are two problems: 1) It lists only the page currently being shown (probably because of the @page=Page.find(params[:id]) line--I need to figure out a way to list all of the pages and 2) Although I''ve established the relationship between content and pages in their respective model pages, I need a way of referencing only the content associated with the page being viewed. I know its basic, but if anyone could throw out some help, I''d appreciate it. Ron --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ron wrote:> Okay, so this is really a beginner''s question. I''m just starting to > move over from ColdFusion to Rails and I''m having some trouble > accessing ActiveRecord objects from within views. > > I''m sure I''m missing something pretty obvious. I have a page model, a > pages_controller, a content model and a content_controller. The idea > is for the content controller to manage the administrator''s > interaction with the content and for pages to display the content to > visitors. > > But I''m having problems referencing the variables inside the > pages_controller. I have: > > def show > @page=Page.find(params[:id]) > @content = Content.find(:all) > end > > I have a partial view _header.rhtml that just has: > > <% for page in @pages %> > <ul> > <li><%=@page.title%></li> > </ul> > <%end%> > > There are two problems: 1) It lists only the page currently being > shown (probably because of the @page=Page.find(params[:id]) line--I > need to figure out a way to list all of the pages and 2) Although I''ve > established the relationship between content and pages in their > respective model pages, I need a way of referencing only the content > associated with the page being viewed. > > I know its basic, but if anyone could throw out some help, I''d > appreciate it. > > Ron > >@pages is nil in your view because you didn''t define in in the controller action, you only defined @page. If you define @pages (possibly with something like @pages = Page.find(:all)) you''ll have more luck. And as a seperate observation... you might want to just make ''content'' a text column on the pages table, and call it with @page.content, instead of making it an entire different class. -- http://www.5valleys.com/ http://www.workingwithrails.com/person/8078 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Jon, Thanks! That''s helpful. I guess my thought behind making pages a separate controller is that I wanted to be able to login at some point down the road and edit the content of pages. For some reason, I got it in my head that it would be better to have a separate controller for the presentation part and another for the content part. I guess if I had just one controller, I''d just change the controller to require authorization to edit? Ron On Mar 5, 4:10 pm, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Ron wrote: > > Okay, so this is really a beginner''s question. I''m just starting to > > move over from ColdFusion to Rails and I''m having some trouble > > accessing ActiveRecord objects from within views. > > > I''m sure I''m missing something pretty obvious. I have a page model, a > > pages_controller, a content model and a content_controller. The idea > > is for the content controller to manage the administrator''s > > interaction with the content and for pages to display the content to > > visitors. > > > But I''m having problems referencing the variables inside the > > pages_controller. I have: > > > def show > > @page=Page.find(params[:id]) > > @content = Content.find(:all) > > end > > > I have a partial view _header.rhtml that just has: > > > <% for page in @pages %> > > <ul> > > <li><%...@page.title%></li> > > </ul> > > <%end%> > > > There are two problems: 1) It lists only the page currently being > > shown (probably because of the @page=Page.find(params[:id]) line--I > > need to figure out a way to list all of the pages and 2) Although I''ve > > established the relationship between content and pages in their > > respective model pages, I need a way of referencing only the content > > associated with the page being viewed. > > > I know its basic, but if anyone could throw out some help, I''d > > appreciate it. > > > Ron > > @pages is nil in your view because you didn''t define in in the > controller action, you only defined @page. If you define @pages > (possibly with something like @pages = Page.find(:all)) you''ll have more > luck. > > And as a seperate observation... you might want to just make ''content'' a > text column on the pages table, and call it with @page.content, instead > of making it an entire different class. > > --http://www.5valleys.com/http://www.workingwithrails.com/person/8078--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Depending on your implementation, you may want to do separate controllers, or maybe not. However, that wouldn''t change how you setup your models. It''s a common misconception I see with people just getting into Rails (I even had it myself once) that there is a 1 to 1 correlation to models and controllers. It''s not uncommon in complex Rails apps however to have some controllers deal with multiple models in a single action and to have several controllers that all share one model but do different things with it. Ron wrote:> Hi Jon, > > Thanks! That''s helpful. I guess my thought behind making pages a > separate controller is that I wanted to be able to login at some point > down the road and edit the content of pages. For some reason, I got > it in my head that it would be better to have a separate controller > for the presentation part and another for the content part. > > I guess if I had just one controller, I''d just change the controller > to require authorization to edit? > > Ron > > On Mar 5, 4:10 pm, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Ron wrote: >> >>> Okay, so this is really a beginner''s question. I''m just starting to >>> move over from ColdFusion to Rails and I''m having some trouble >>> accessing ActiveRecord objects from within views. >>> >>> I''m sure I''m missing something pretty obvious. I have a page model, a >>> pages_controller, a content model and a content_controller. The idea >>> is for the content controller to manage the administrator''s >>> interaction with the content and for pages to display the content to >>> visitors. >>> >>> But I''m having problems referencing the variables inside the >>> pages_controller. I have: >>> >>> def show >>> @page=Page.find(params[:id]) >>> @content = Content.find(:all) >>> end >>> >>> I have a partial view _header.rhtml that just has: >>> >>> <% for page in @pages %> >>> <ul> >>> <li><%...@page.title%></li> >>> </ul> >>> <%end%> >>> >>> There are two problems: 1) It lists only the page currently being >>> shown (probably because of the @page=Page.find(params[:id]) line--I >>> need to figure out a way to list all of the pages and 2) Although I''ve >>> established the relationship between content and pages in their >>> respective model pages, I need a way of referencing only the content >>> associated with the page being viewed. >>> >>> I know its basic, but if anyone could throw out some help, I''d >>> appreciate it. >>> >>> Ron >>> >> @pages is nil in your view because you didn''t define in in the >> controller action, you only defined @page. If you define @pages >> (possibly with something like @pages = Page.find(:all)) you''ll have more >> luck. >> >> And as a seperate observation... you might want to just make ''content'' a >> text column on the pages table, and call it with @page.content, instead >> of making it an entire different class. >> >> --http://www.5valleys.com/http://www.workingwithrails.com/person/8078 >> > > > >-- http://www.5valleys.com/ http://www.workingwithrails.com/person/8078 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Jon! That really helps. Ron On Mar 6, 8:23 am, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Depending on your implementation, you may want to do separate > controllers, or maybe not. However, that wouldn''t change how you setup > your models. It''s a common misconception I see with people just getting > into Rails (I even had it myself once) that there is a 1 to 1 > correlation to models and controllers. It''s not uncommon in complex > Rails apps however to have some controllers deal with multiple models in > a single action and to have several controllers that all share one model > but do different things with it. > > > > Ron wrote: > > Hi Jon, > > > Thanks! That''s helpful. I guess my thought behind making pages a > > separate controller is that I wanted to be able to login at some point > > down the road and edit the content of pages. For some reason, I got > > it in my head that it would be better to have a separate controller > > for the presentation part and another for the content part. > > > I guess if I had just one controller, I''d just change the controller > > to require authorization to edit? > > > Ron > > > On Mar 5, 4:10 pm, Jon Garvin <jgarvin.li...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> Ron wrote: > > >>> Okay, so this is really a beginner''s question. I''m just starting to > >>> move over from ColdFusion to Rails and I''m having some trouble > >>> accessing ActiveRecord objects from within views. > > >>> I''m sure I''m missing something pretty obvious. I have a page model, a > >>> pages_controller, a content model and a content_controller. The idea > >>> is for the content controller to manage the administrator''s > >>> interaction with the content and for pages to display the content to > >>> visitors. > > >>> But I''m having problems referencing the variables inside the > >>> pages_controller. I have: > > >>> def show > >>> @page=Page.find(params[:id]) > >>> @content = Content.find(:all) > >>> end > > >>> I have a partial view _header.rhtml that just has: > > >>> <% for page in @pages %> > >>> <ul> > >>> <li><%...@page.title%></li> > >>> </ul> > >>> <%end%> > > >>> There are two problems: 1) It lists only the page currently being > >>> shown (probably because of the @page=Page.find(params[:id]) line--I > >>> need to figure out a way to list all of the pages and 2) Although I''ve > >>> established the relationship between content and pages in their > >>> respective model pages, I need a way of referencing only the content > >>> associated with the page being viewed. > > >>> I know its basic, but if anyone could throw out some help, I''d > >>> appreciate it. > > >>> Ron > > >> @pages is nil in your view because you didn''t define in in the > >> controller action, you only defined @page. If you define @pages > >> (possibly with something like @pages = Page.find(:all)) you''ll have more > >> luck. > > >> And as a seperate observation... you might want to just make ''content'' a > >> text column on the pages table, and call it with @page.content, instead > >> of making it an entire different class. > > >> --http://www.5valleys.com/http://www.workingwithrails.com/person/8078 > > --http://www.5valleys.com/http://www.workingwithrails.com/person/8078--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---