Kristian Mandrup
2010-May-13 19:19 UTC
Request: Make UrlHelper methods available for controllers
I had a need for accessing the methods in ActionView::Helpers::UrlHelper in my controllers to conditionally return link HTML to my views depending on login state etc. I think that the UrlHelper should by default be added to all Controllers and methods such as link_to made available for views using helper_method :link_to. What do you think? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Kristian Mandrup
2010-May-13 19:52 UTC
Re: Request: Make UrlHelper methods available for controllers
I''m having problems using url_for in a controller. The original method looks like this, but the @controller variable is unknown in this context. However if I remove it, it goes in an endless loop for the Hash case. Anyone? def url_for(options = {}) options ||= {} url = case options when String escape = true options when Hash options = { :only_path => options[:host].nil? }.update(options.symbolize_keys) escape = options.key?(:escape) ? options.delete(:escape) : true @controller.send(:url_for, options) when :back escape = false @controller.request.env["HTTP_REFERER"] || ''javascript:history.back()'' else escape = false polymorphic_path(options) end escape ? escape_once(url) : url end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Allen Madsen
2010-May-13 21:54 UTC
Re: Re: Request: Make UrlHelper methods available for controllers
Kristian, If I understand what you are trying to do, then its a little backwards. You really shouldn''t be generating view stuff in the controller. Instead you can create a class variable like @logged_in and then use that to selectively show certain components in the view. Allen Madsen http://www.allenmadsen.com On Thu, May 13, 2010 at 3:52 PM, Kristian Mandrup <kmandrup@gmail.com> wrote:> I''m having problems using url_for in a controller. The original method > looks like this, but the @controller variable is unknown in this > context. However if I remove it, it goes in an endless loop for the > Hash case. Anyone? > > def url_for(options = {}) > options ||= {} > url = case options > when String > escape = true > options > when Hash > options = { :only_path => > options[:host].nil? }.update(options.symbolize_keys) > escape = options.key?(:escape) ? options.delete(:escape) : > true > @controller.send(:url_for, options) > when :back > escape = false > @controller.request.env["HTTP_REFERER"] || > ''javascript:history.back()'' > else > escape = false > polymorphic_path(options) > end > > escape ? escape_once(url) : url > end > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.