I''m fairly new to RoR, so apologies in advance if I rape the terminology here... I am trying to use functionality from one controller in another controller. Here is the first controller: class PagesController < ApplicationController def show @page = Page.find(params[:id]) end end 2nd controller class SiteController < ApplicationController def some_page @page = Page.find(:all, :conditions => "name = ''some_page''") end end Ultiamtely, I''m trying to access my "Pages" table from a controller other than the Pages controller... not sure if I have to extend the new controller or how the functionality "trickles down" to my new controller? Any advice is appreciated --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You don''t need to do anything in particular. The Page model is available in the same way from whatever controller or other model you''re using. Fred --~--~---------~--~----~------------~-------~--~----~ 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 for the response, that''s what I was hoping was the case. One more question: This code in the "show" Page view works fine <% for column in Page.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h @page.send(column.name) %> </p> <% end %> However if I copy that into the "about" view in the new controller, I get this error: undefined method `name'' for #<Array:0xb799d1f0> Note, I''m using this to fetch the data in the controller: @page Page.find(:all, :conditions => "name = ''some_page''") <%= debug(@page) %> shows a valid record returned in the Page object, so I''m unclear why the for loop would be failing? thanks, Eben On Sep 30, 1:16 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You don''t need to do anything in particular. The Page model is > available in the same way from whatever controller or other model > you''re using. > > Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The problem is that in your show action, @page is a single record. In your "about" action, you are using :all in find, which returns an array. If you just want a single record, change it to :first. If you want to use the array, do it like this: <% @pages.each do |page| -%> <% for column in Page.content_columns %> <p> <b><%= column.human_name %>:</b> <%=h page.send(column.name) %> </p> <% end %> <% end -%> eben wrote:> Thanks for the response, that''s what I was hoping was the case. One > more question: > > This code in the "show" Page view works fine > > <% for column in Page.content_columns %> > <p> > <b><%= column.human_name %>:</b> <%=h @page.send(column.name) %> > </p> > <% end %> > > However if I copy that into the "about" view in the new controller, I > get this error: > > undefined method `name'' for #<Array:0xb799d1f0> > > Note, I''m using this to fetch the data in the controller: @page > Page.find(:all, :conditions => "name = ''some_page''") > > <%= debug(@page) %> shows a valid record returned in the Page object, > so I''m unclear why the for loop would be failing? > > thanks, > Eben > > On Sep 30, 1:16 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > >> You don''t need to do anything in particular. The Page model is >> available in the same way from whatever controller or other model >> you''re using. >> >> Fred >> > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
makes sense, thanks for your time Eben On Sep 30, 1:51 pm, William Pratt <bi...-YbheRAKfYF4eIZ0/mPfg9Q@public.gmane.org> wrote:> The problem is that in your show action, @page is a single record. In > your "about" action, you are using :all in find, which returns an array. > If you just want a single record, change it to :first. If you want to > use the array, do it like this: > > <% @pages.each do |page| -%> > <% for column in Page.content_columns %> > <p> > <b><%= column.human_name %>:</b> <%=h page.send(column.name) %> > </p> > <% end %> > <% end -%> > > eben wrote: > > Thanks for the response, that''s what I was hoping was the case. One > > more question: > > > This code in the "show" Page view works fine > > > <% for column in Page.content_columns %> > > <p> > > <b><%= column.human_name %>:</b> <%=h @page.send(column.name) %> > > </p> > > <% end %> > > > However if I copy that into the "about" view in the new controller, I > > get this error: > > > undefined method `name'' for #<Array:0xb799d1f0> > > > Note, I''m using this to fetch the data in the controller: @page > > Page.find(:all, :conditions => "name = ''some_page''") > > > <%= debug(@page) %> shows a valid record returned in the Page object, > > so I''m unclear why the for loop would be failing? > > > thanks, > > Eben > > > On Sep 30, 1:16 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > >> You don''t need to do anything in particular. The Page model is > >> available in the same way from whatever controller or other model > >> you''re using. > > >> Fred--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---