Hi Jim and Everyone who is interested in component in rails, I am really interested in the component idea, but I am encounting some problems. Tried archive, and read the Agile Web ... book, but no luck so far. Any suggestion, advice, solution, experience, work around are appreciated :) Problem: Can''t use a compoent(in component directory) to call it''s own controller''s action using link_to_remote, and periodically_call_remote. It calls the controller from a root context instead. Example: (Silly, but prove my point) A very simple TimeTicker component that uses link_to_remote to call it''s action show_time, and update the component that''s rendered on the current page without page refresh. I intend to make this component to call it''s own controller action, which is show_time, but it is calling /root/show_time. <a href="#" onclick="new Ajax.Updater(''ajax_time_ticker'', ''/root/show_time'', {asynchronous:true, evalScripts:true}); return false;">click here</a> I followed the Book: Agile Web Development with Rails, and http://manuals.rubyonrails.com/read/book/14. Below is the code. ...component/TimeTicker ajax_time_ticker/index.rhtml ajax_time_ticker_controller.rb ...app/controllers/root_controller.rb ...app/views/root/index.rhtml ajax_time_ticker/index.rhtml: -------------------------------------------------------------------------------------------------------- <div id="ajax_time_ticker"></div> <%= link_to_remote( "click here", :update => "ajax_time_ticker", :url => { :action => :show_time } ) %> -------------------------------------------------------------------------------------------------------- ajax_time_ticker_controller.rb -------------------------------------------------------------------------------------------------------- class Timeticker::AjaxTimeTickerController < ActionController::Base uses_component_template_root def index end def show_time render :text => DateTime.now.to_s, :layout => false end end controllers/root_controller.rb -------------------------------------------------------------------------------------------------------- class RootController < ApplicationController def index @title = "pt" end end views/root/index.rhtml -------------------------------------------------------------------------------------------------------- <%= render_component(:controller => "Timeticker/AjaxTimeTicker", :action => "index") %>
Craig Ambrose
2005-Nov-02 03:15 UTC
Re: component remotely call it''s own contoller doesn''t work
Hi Sun, I''ve hit very much the same problem. I''ve temporarily solved it by passing the base url into the component, so that it can create all links relative to that (which typically involves creating my links manually, rather that user routes). I''ve discussed changing the behaviour so that component links are expressed in a relative manner, but unfortunatelly not all are happy with this solution, as it would definatelly change the way components behave (for better or worse) and so might render some existing components unuseable. However, the new plugins system does mean that this change could be implemented in a way which needn''t affect everyone. I''d be interested in talking to you further about solutions to this. Perhaps we could collaborate on a plugin? regards, Craig Lei Sun wrote:>Hi Jim and Everyone who is interested in component in rails, > >I am really interested in the component idea, but I am encounting some >problems. Tried archive, and read the Agile Web ... book, but no luck >so far. Any suggestion, advice, solution, experience, work around are >appreciated :) > >Problem: >Can''t use a compoent(in component directory) to call it''s own >controller''s action using >link_to_remote, and periodically_call_remote. It calls the controller >from a root context instead. > >Example: (Silly, but prove my point) >A very simple TimeTicker component that uses link_to_remote to call >it''s action show_time, and update the component that''s rendered on the >current page without page refresh. > >I intend to make this component to call it''s own controller action, >which is show_time, but it is calling /root/show_time. > ><a href="#" onclick="new Ajax.Updater(''ajax_time_ticker'', >''/root/show_time'', {asynchronous:true, evalScripts:true}); return >false;">click here</a> > >I followed the Book: Agile Web Development with Rails, and >http://manuals.rubyonrails.com/read/book/14. > >Below is the code. > >...component/TimeTicker > ajax_time_ticker/index.rhtml > ajax_time_ticker_controller.rb > >...app/controllers/root_controller.rb >...app/views/root/index.rhtml > >ajax_time_ticker/index.rhtml: >-------------------------------------------------------------------------------------------------------- ><div id="ajax_time_ticker"></div> ><%= link_to_remote( "click here", :update => "ajax_time_ticker", > :url => { :action => :show_time } ) %> >-------------------------------------------------------------------------------------------------------- > >ajax_time_ticker_controller.rb >-------------------------------------------------------------------------------------------------------- >class Timeticker::AjaxTimeTickerController < ActionController::Base > uses_component_template_root > def index > end > def show_time > render :text => DateTime.now.to_s, :layout => false > end >end > >controllers/root_controller.rb >-------------------------------------------------------------------------------------------------------- >class RootController < ApplicationController > def index > @title = "pt" > end >end > >views/root/index.rhtml >-------------------------------------------------------------------------------------------------------- ><%= render_component(:controller => "Timeticker/AjaxTimeTicker", > :action => "index") %> >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > >
Lei Sun
2005-Nov-02 04:12 UTC
Re: component remotely call it''s own contoller doesn''t work
Definitely interested :) Lei On 11/1/05, Craig Ambrose <craig-sZi9aYDroxztFRKb8DbDFhCuuivNXqWP@public.gmane.org> wrote:> Hi Sun, > > I''ve hit very much the same problem. I''ve temporarily solved it by > passing the base url into the component, so that it can create all links > relative to that (which typically involves creating my links manually, > rather that user routes). I''ve discussed changing the behaviour so that > component links are expressed in a relative manner, but unfortunatelly > not all are happy with this solution, as it would definatelly change the > way components behave (for better or worse) and so might render some > existing components unuseable. However, the new plugins system does mean > that this change could be implemented in a way which needn''t affect > everyone. > > I''d be interested in talking to you further about solutions to this. > Perhaps we could collaborate on a plugin? > > regards, > > Craig > > Lei Sun wrote: > > >Hi Jim and Everyone who is interested in component in rails, > > > >I am really interested in the component idea, but I am encounting some > >problems. Tried archive, and read the Agile Web ... book, but no luck > >so far. Any suggestion, advice, solution, experience, work around are > >appreciated :) > > > >Problem: > >Can''t use a compoent(in component directory) to call it''s own > >controller''s action using > >link_to_remote, and periodically_call_remote. It calls the controller > >from a root context instead. > > > >Example: (Silly, but prove my point) > >A very simple TimeTicker component that uses link_to_remote to call > >it''s action show_time, and update the component that''s rendered on the > >current page without page refresh. > > > >I intend to make this component to call it''s own controller action, > >which is show_time, but it is calling /root/show_time. > > > ><a href="#" onclick="new Ajax.Updater(''ajax_time_ticker'', > >''/root/show_time'', {asynchronous:true, evalScripts:true}); return > >false;">click here</a> > > > >I followed the Book: Agile Web Development with Rails, and > >http://manuals.rubyonrails.com/read/book/14. > > > >Below is the code. > > > >...component/TimeTicker > > ajax_time_ticker/index.rhtml > > ajax_time_ticker_controller.rb > > > >...app/controllers/root_controller.rb > >...app/views/root/index.rhtml > > > >ajax_time_ticker/index.rhtml: > >-------------------------------------------------------------------------------------------------------- > ><div id="ajax_time_ticker"></div> > ><%= link_to_remote( "click here", :update => "ajax_time_ticker", > > :url => { :action => :show_time } ) %> > >-------------------------------------------------------------------------------------------------------- > > > >ajax_time_ticker_controller.rb > >-------------------------------------------------------------------------------------------------------- > >class Timeticker::AjaxTimeTickerController < ActionController::Base > > uses_component_template_root > > def index > > end > > def show_time > > render :text => DateTime.now.to_s, :layout => false > > end > >end > > > >controllers/root_controller.rb > >-------------------------------------------------------------------------------------------------------- > >class RootController < ApplicationController > > def index > > @title = "pt" > > end > >end > > > >views/root/index.rhtml > >-------------------------------------------------------------------------------------------------------- > ><%= render_component(:controller => "Timeticker/AjaxTimeTicker", > > :action => "index") %> > >_______________________________________________ > >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 >
Lee Marlow
2005-Nov-02 16:07 UTC
Re: component remotely call it''s own contoller doesn''t work
I was having the same problem. My current work-around is to include the controller in the options. Try this for your link_to_remote: <%= link_to_remote( "click here", :update => "ajax_time_ticker", :url => { :controller => "/#{controller.class.controller_path}", :action => :show_time } ) %> The leading slash is what makes it work. This is due to some URL rewriting done in Routing, that I haven''t figured out yet. -Lee On 11/1/05, Lei Sun <lei.sun-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Jim and Everyone who is interested in component in rails, > > I am really interested in the component idea, but I am encounting some > problems. Tried archive, and read the Agile Web ... book, but no luck > so far. Any suggestion, advice, solution, experience, work around are > appreciated :) > > Problem: > Can''t use a compoent(in component directory) to call it''s own > controller''s action using > link_to_remote, and periodically_call_remote. It calls the controller > from a root context instead. > > Example: (Silly, but prove my point) > A very simple TimeTicker component that uses link_to_remote to call > it''s action show_time, and update the component that''s rendered on the > current page without page refresh. > > I intend to make this component to call it''s own controller action, > which is show_time, but it is calling /root/show_time. > > <a href="#" onclick="new Ajax.Updater(''ajax_time_ticker'', > ''/root/show_time'', {asynchronous:true, evalScripts:true}); return > false;">click here</a> > > I followed the Book: Agile Web Development with Rails, and > http://manuals.rubyonrails.com/read/book/14. > > Below is the code. > > ...component/TimeTicker > ajax_time_ticker/index.rhtml > ajax_time_ticker_controller.rb > > ...app/controllers/root_controller.rb > ...app/views/root/index.rhtml > > ajax_time_ticker/index.rhtml: > -------------------------------------------------------------------------------------------------------- > <div id="ajax_time_ticker"></div> > <%= link_to_remote( "click here", :update => "ajax_time_ticker", > :url => { :action => :show_time } ) %> > -------------------------------------------------------------------------------------------------------- > > ajax_time_ticker_controller.rb > -------------------------------------------------------------------------------------------------------- > class Timeticker::AjaxTimeTickerController < ActionController::Base > uses_component_template_root > def index > end > def show_time > render :text => DateTime.now.to_s, :layout => false > end > end > > controllers/root_controller.rb > -------------------------------------------------------------------------------------------------------- > class RootController < ApplicationController > def index > @title = "pt" > end > end > > views/root/index.rhtml > -------------------------------------------------------------------------------------------------------- > <%= render_component(:controller => "Timeticker/AjaxTimeTicker", > :action => "index") %> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Lei Sun
2005-Nov-02 23:30 UTC
Re: component remotely call it''s own contoller doesn''t work
Hi Guys, Both of your methods 1. passing the base dir name into the component 2. just construct the entire string path to the controller 3. I even tried to construct the path to the controller by modifying the routes.rb doesn''t seems to work for my case, since I isolated the components that I created to the component directory, which resides outside of the app directory and public directory. So there is no way to Is it possible to add a parameter -component_name- to the remote_function, url_for, link_to, as an optional parameter. When called, it will call the appropriate controller inside the component, instead of calling controller inside / Can anyone help me out? Thanks in advance On 11/2/05, Lee Marlow <lee.marlow-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I was having the same problem. My current work-around is to include > the controller in the options. Try this for your link_to_remote: > > <%= link_to_remote( "click here", :update => "ajax_time_ticker", > :url => { > :controller => "/#{controller.class.controller_path}", > :action => :show_time } ) %> > > The leading slash is what makes it work. This is due to some URL > rewriting done in Routing, that I haven''t figured out yet. > > -Lee > > On 11/1/05, Lei Sun <lei.sun-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi Jim and Everyone who is interested in component in rails, > > > > I am really interested in the component idea, but I am encounting some > > problems. Tried archive, and read the Agile Web ... book, but no luck > > so far. Any suggestion, advice, solution, experience, work around are > > appreciated :) > > > > Problem: > > Can''t use a compoent(in component directory) to call it''s own > > controller''s action using > > link_to_remote, and periodically_call_remote. It calls the controller > > from a root context instead. > > > > Example: (Silly, but prove my point) > > A very simple TimeTicker component that uses link_to_remote to call > > it''s action show_time, and update the component that''s rendered on the > > current page without page refresh. > > > > I intend to make this component to call it''s own controller action, > > which is show_time, but it is calling /root/show_time. > > > > <a href="#" onclick="new Ajax.Updater(''ajax_time_ticker'', > > ''/root/show_time'', {asynchronous:true, evalScripts:true}); return > > false;">click here</a> > > > > I followed the Book: Agile Web Development with Rails, and > > http://manuals.rubyonrails.com/read/book/14. > > > > Below is the code. > > > > ...component/TimeTicker > > ajax_time_ticker/index.rhtml > > ajax_time_ticker_controller.rb > > > > ...app/controllers/root_controller.rb > > ...app/views/root/index.rhtml > > > > ajax_time_ticker/index.rhtml: > > -------------------------------------------------------------------------------------------------------- > > <div id="ajax_time_ticker"></div> > > <%= link_to_remote( "click here", :update => "ajax_time_ticker", > > :url => { :action => :show_time } ) %> > > -------------------------------------------------------------------------------------------------------- > > > > ajax_time_ticker_controller.rb > > -------------------------------------------------------------------------------------------------------- > > class Timeticker::AjaxTimeTickerController < ActionController::Base > > uses_component_template_root > > def index > > end > > def show_time > > render :text => DateTime.now.to_s, :layout => false > > end > > end > > > > controllers/root_controller.rb > > -------------------------------------------------------------------------------------------------------- > > class RootController < ApplicationController > > def index > > @title = "pt" > > end > > end > > > > views/root/index.rhtml > > -------------------------------------------------------------------------------------------------------- > > <%= render_component(:controller => "Timeticker/AjaxTimeTicker", > > :action => "index") %> > > _______________________________________________ > > 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 >
Craig Ambrose
2005-Nov-03 02:58 UTC
Re: component remotely call it''s own contoller doesn''t work
That''s quite a good idea Lei, however, I''m not sure how to get such a thing to work. You would need to have a routes system inside the component. Otherwise, building a url_for for the specified -component_name- would still use the base application route, which would generally not work for the component, except in the most trivial of situations. For everyone else''s benefit, I sent the following thoughts to Lei earlier regarding components with routes: - If components have routes, and since they already have their own controllers, views and models, is there any difference between components and whole rails applications? Perhaps the ''component'' system which I am after should instead render an entire nested rails application and place the result in the specified place in the parent app. If so, I guess they would want to use the already open database connection. This would have a couple of advantages of existing components, such as: * These ''component applications'' would be testable. Currently, rails functional tests cannot be written for components. If a component was an application, it could be tested in isolation, and normal rails testing would apply. Also, the tests would be packaged with the component application, rather than needing to be added to the parent. * Component applications would be easier to develop if they also ran as stand alone apps. Perhaps they could know if they were being run as a component, so that they could decide to use a different layout template (thus enabling them to generate html headers if not a component, and not do so if they are). Craig Lei Sun wrote:>Is it possible to add a parameter -component_name- to the >remote_function, url_for, link_to, as an optional parameter. When >called, it will call the appropriate controller inside the component, >instead of calling controller inside / > >