Hi All, Does anybody know if it''s possible to override the layout used by a controller when it is rendered as a component? Something like: <%= render_component(:controller => ''messages'', :action => ''tree'', :layout => ''scaffold'') %> I''m trying to pull in a little discussion component, that gives a short threaded discussion in tree format at the bottom of pages. Thing is that it also pulls in my three pane layout, banner etc. I''d like the layout to be the full three panes etc when I just look at the messages directly, but I''d like just the basic message tree without surrounding layout when I pull them into another page. Maybe I''m just going about this wrong, and perhaps I should be bringing in the layouts at a higher level, but the whole setup in rails seems to be that a page will be the result of a controller interacting with a layout and a model, which is fair enough, but if that''s so it would be nice to be able to mix and match ... In addition the pagination part that gets pulled in with the component is linked to the main controller for the page, not the rendered component controller, which is not quite what a component should do. Any ideas on any of this greatly appreciated. CHEERS> SAM
The Rails book covers this exact situation. In section 9.2 in advises that, when you insert the component, pass it a parameter such as '':context => :small'' or somesuch. Then, in the controller for that component, have an if statement such as ''if params[:context] == :small then render(:layout => false)''. With that explicit render call and its disabling of layouts, you should get the results you want, if I understand your problem correctly. On 9/16/05, Sam Joseph <sam-wUweuJJ0J032eFz/2MeuCQ@public.gmane.org> wrote:> Hi All, > > Does anybody know if it''s possible to override the layout used by a > controller when it is rendered as a component? Something like: > > <%= render_component(:controller => ''messages'', :action => ''tree'', > :layout => ''scaffold'') %> > > I''m trying to pull in a little discussion component, that gives a short > threaded discussion in tree format at the bottom of pages. Thing is that > it also pulls in my three pane layout, banner etc. > > I''d like the layout to be the full three panes etc when I just look at > the messages directly, but I''d like just the basic message tree without > surrounding layout when I pull them into another page. > > Maybe I''m just going about this wrong, and perhaps I should be bringing > in the layouts at a higher level, but the whole setup in rails seems to > be that a page will be the result of a controller interacting with a > layout and a model, which is fair enough, but if that''s so it would be > nice to be able to mix and match ... > > In addition the pagination part that gets pulled in with the component > is linked to the main controller for the page, not the rendered > component controller, which is not quite what a component should do. > > Any ideas on any of this greatly appreciated. > > CHEERS> SAM > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -Matt Torok
Hi Matt, Many thanks - that fixes the problem. We have the rails book on order from Amazon, should arrive next week. I was hoping this techinque might also fix my pagination problem (inserted component pagination uses name of the collection of the view into which it was inserted), e.g. If I do the following in the resources list view: <%= render_component(:controller => ''messages'', :action => ''tree'', :params => {:context => ''small''}) %> while the messages appear, their paginaton links are set to: /resources?page=... when they should be /messages?page=... but nothing I''ve tried so far seems to fix this, any ideas? Many thanks in advance. CHEERS> SAM Matt Torok wrote:>The Rails book covers this exact situation. In section 9.2 in advises >that, when you insert the component, pass it a parameter such as >'':context => :small'' or somesuch. Then, in the controller for that >component, have an if statement such as ''if params[:context] == :small >then render(:layout => false)''. With that explicit render call and its >disabling of layouts, you should get the results you want, if I >understand your problem correctly. > >On 9/16/05, Sam Joseph <sam-wUweuJJ0J032eFz/2MeuCQ@public.gmane.org> wrote: > > >>Hi All, >> >>Does anybody know if it''s possible to override the layout used by a >>controller when it is rendered as a component? Something like: >> >><%= render_component(:controller => ''messages'', :action => ''tree'', >>:layout => ''scaffold'') %> >> >>I''m trying to pull in a little discussion component, that gives a short >>threaded discussion in tree format at the bottom of pages. Thing is that >>it also pulls in my three pane layout, banner etc. >> >>I''d like the layout to be the full three panes etc when I just look at >>the messages directly, but I''d like just the basic message tree without >>surrounding layout when I pull them into another page. >> >>Maybe I''m just going about this wrong, and perhaps I should be bringing >>in the layouts at a higher level, but the whole setup in rails seems to >>be that a page will be the result of a controller interacting with a >>layout and a model, which is fair enough, but if that''s so it would be >>nice to be able to mix and match ... >> >>In addition the pagination part that gets pulled in with the component >>is linked to the main controller for the page, not the rendered >>component controller, which is not quite what a component should do. >> >>Any ideas on any of this greatly appreciated. >> >>CHEERS> SAM >> >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > > > >
Sam. Hmm, I''m a little confused on the specifics of your setup. As I understand it, you have a view that shows your messages that is paginated. You also have another view that does something else and inserts the first view into itself. The problem you''re having is that, when inserted, the paginated links of the first (messages) view link to the second (outer) view when you want them to link to the first view. Is this correct? If so, my first advice would be to consider your design. If you have paginated links that link to another controller, it may be better to either redesign it so that there are no paginated links or to have the second view (the one that is currently the top-level view) be the view that is inserted into another view. The reason is that users usually perceive pagination links as something that will keep them ''in context'' and not take them somewhere completely different. Of course, I can''t see your layout and it may be perfectly fine how it is, but speaking in generalities that''s my opinion. But if you do think it works how it is, you could try manually creating a Paginator object as described here: http://api.rubyonrails.com/classes/ActionController/Pagination/Paginator.html . It looks as if it allows you to manually set the controller to which to paginate. I haven''t personally used that method, so I can''t give you an authoritative answer, but it appears to solve your problem. Hope this helps. On 9/18/05, Sam Joseph <sam-wUweuJJ0J032eFz/2MeuCQ@public.gmane.org> wrote:> Hi Matt, > > Many thanks - that fixes the problem. We have the rails book on order > from Amazon, should arrive next week. > > I was hoping this techinque might also fix my pagination problem > (inserted component pagination uses name of the collection of the view > into which it was inserted), e.g. > > If I do the following in the resources list view: > > <%= render_component(:controller => ''messages'', :action => ''tree'', > :params => {:context => ''small''}) %> > > while the messages appear, their paginaton links are set to: > > /resources?page=... > > when they should be > > /messages?page=... > > but nothing I''ve tried so far seems to fix this, any ideas? > > Many thanks in advance. > > CHEERS> SAM > > > Matt Torok wrote: > > >The Rails book covers this exact situation. In section 9.2 in advises > >that, when you insert the component, pass it a parameter such as > >'':context => :small'' or somesuch. Then, in the controller for that > >component, have an if statement such as ''if params[:context] == :small > >then render(:layout => false)''. With that explicit render call and its > >disabling of layouts, you should get the results you want, if I > >understand your problem correctly. > > > >On 9/16/05, Sam Joseph <sam-wUweuJJ0J032eFz/2MeuCQ@public.gmane.org> wrote: > > > > > >>Hi All, > >> > >>Does anybody know if it''s possible to override the layout used by a > >>controller when it is rendered as a component? Something like: > >> > >><%= render_component(:controller => ''messages'', :action => ''tree'', > >>:layout => ''scaffold'') %> > >> > >>I''m trying to pull in a little discussion component, that gives a short > >>threaded discussion in tree format at the bottom of pages. Thing is that > >>it also pulls in my three pane layout, banner etc. > >> > >>I''d like the layout to be the full three panes etc when I just look at > >>the messages directly, but I''d like just the basic message tree without > >>surrounding layout when I pull them into another page. > >> > >>Maybe I''m just going about this wrong, and perhaps I should be bringing > >>in the layouts at a higher level, but the whole setup in rails seems to > >>be that a page will be the result of a controller interacting with a > >>layout and a model, which is fair enough, but if that''s so it would be > >>nice to be able to mix and match ... > >> > >>In addition the pagination part that gets pulled in with the component > >>is linked to the main controller for the page, not the rendered > >>component controller, which is not quite what a component should do. > >> > >>Any ideas on any of this greatly appreciated. > >> > >>CHEERS> SAM > >> > >>_______________________________________________ > >>Rails mailing list > >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >>http://lists.rubyonrails.org/mailman/listinfo/rails > >> > >> > >> > > > > > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -Matt Torok
Hi Matt, Matt Torok wrote:>Hmm, I''m a little confused on the specifics of your setup. As I >understand it, you have a view that shows your messages that is >paginated. You also have another view that does something else and >inserts the first view into itself. The problem you''re having is that, >when inserted, the paginated links of the first (messages) view link >to the second (outer) view when you want them to link to the first >view. Is this correct? > >This is correct>If so, my first advice would be to consider your design. If you have >paginated links that link to another controller, it may be better to >either redesign it so that there are no paginated links or to have the >second view (the one that is currently the top-level view) be the view >that is inserted into another view. The reason is that users usually >perceive pagination links as something that will keep them ''in >context'' and not take them somewhere completely different. Of course, >I can''t see your layout and it may be perfectly fine how it is, but >speaking in generalities that''s my opinion. > >This makes a good deal of sense, although I believe the pagination as I''m using it should keep them in context. The idea is that you have a list of resources (stories, bookmarks whatever), and you have discussions taking place about each - a bit like the discussions associated with each entry in the php manual, e.g. http://us3.php.net/manual/en/function.http-build-query.php so the user is naturally looking through the resources that other users have entered, and when they view an individual resource, the show view includes the discussion (i.e. the messages) associated with it. If there are a lot of messages with associated with a single resource I thought it would be nice to paginate them. Following your suggestion an alternative approach would be to run this all through the message controller instead of the resource controller, and display individual resources as components in the message view. This is a good idea and I belive we will do this as well, but I had been hoping to create multiple perspectives, of which a paginated set of messages associated with each individual resource was one.>But if you do think it works how it is, you could try manually >creating a Paginator object as described here: >http://api.rubyonrails.com/classes/ActionController/Pagination/Paginator.html >. It looks as if it allows you to manually set the controller to >which to paginate. I haven''t personally used that method, so I can''t >give you an authoritative answer, but it appears to solve your >problem. >Many thanks for your tip here - even though I''d read this before it hadn''t occurred to me to use it in this way. However I am having no luck getting this to work: def tree @message_pages = Paginator.new self, Message.count, 5, @params[''page''] @message = Message.find(:all, :conditions => [ "parent_id IS NULL" ], @message_pages.current.to_sql) #@message_pages, @messages = paginate :message, :per_page => 5, :conditions => [ "parent_id IS NULL" ] if params[:context] == ''small'' render :layout => false end end gives me the error /app/controllers/messages_controller.rb:14: syntax error |routing.rb:219:in `traverse_to_controller'' generated/routing/recognition.rb:47:in `eval'' generated/routing/recognition.rb:47:in `recognize_path'' script/server:51 where line 14 is |@message = Message.find(:all, :conditions => [ "parent_id IS NULL" ], @message_pages.current.to_sql) There seems to be some disconnect between the example for Paginator in the api and the operations available on ActiveRecord, since I can''t see a find_all method at all, which is the one used in the original example. So anyways, many thanks for all your help - I don''t expect you to necessarily have an answer to this latest issue - I''ll keep banging away at it, but I wanted to document how far I got, as I did read about other people having these kinds of problems in the wiki notes. CHERES> SAM
To answer my own question I got the following working thus: def tree @message_pages = Paginator.new self, Message.count, 5, @params[''page''] @messages = Message.find_all "parent_id IS NULL", ''creation_date'', @message_pages.current.to_sql if params[:context] == ''small'' render :layout => false end end However I still can''t get the paginator controller to be anything other than the one for the page that the component is being pulled into: def tree @message_pages = Paginator.new MessagesController.new, Message.count, 5, @params[''page''] @messages = Message.find_all "parent_id IS NULL", ''creation_date'', @message_pages.current.to_sql if params[:context] == ''small'' render :layout => false end end seems to have no effect. Nothing for it but to redesign so that the view that includes the pagination goes through the message controller. And actually that shouldn''t be such a problem. CHEERS> SAM Sam Joseph wrote:> Many thanks for your tip here - even though I''d read this before it > hadn''t occurred to me to use it in this way. However I am having no > luck getting this to work: > > def tree > @message_pages = Paginator.new self, Message.count, 5, @params[''page''] > @message = Message.find(:all, :conditions => [ "parent_id IS NULL" > ], @message_pages.current.to_sql) > #@message_pages, @messages = paginate :message, :per_page => 5, > :conditions => [ "parent_id IS NULL" ] > if params[:context] == ''small'' > render :layout => false > end > end > > gives me the error > > /app/controllers/messages_controller.rb:14: syntax error > |routing.rb:219:in `traverse_to_controller'' > generated/routing/recognition.rb:47:in `eval'' > generated/routing/recognition.rb:47:in `recognize_path'' > script/server:51 > > where line 14 is > > |@message = Message.find(:all, :conditions => [ "parent_id IS NULL" ], > @message_pages.current.to_sql) > > There seems to be some disconnect between the example for Paginator in > the api and the operations available on ActiveRecord, since I can''t > see a find_all method at all, which is the one used in the original > example. > > So anyways, many thanks for all your help - I don''t expect you to > necessarily have an answer to this latest issue - I''ll keep banging > away at it, but I wanted to document how far I got, as I did read > about other people having these kinds of problems in the wiki notes. > > CHERES> SAM > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >