Kristian Mandrup
2010-May-13 20:41 UTC
What is the deal with Controller additions and helper methods!?
I have ben struggling the past two days getting a this tiny piece of functionality to work without any success :( Trying to add a simple helper method to my views in order to ensure links are only displayed if the user has the proper rights to access that functionality. def show_link(object, label = nil) label ||= auth_labels[:show] if can?(:read, object) link_to(label, object) end end But using link_to from within this context, somehow I don''t have access to the controller variable used internally, fx in url_for. def url_for(options = {}) ... when :back escape = false controller.request.env["HTTP_REFERER"] || ''javascript:history.back()'' else ... end If I output self in the show_link method, I can see that my current context of self is ProjectsController, so I guess it makes sense that I don''t have a method controller inside a controller? I access my helper method from here: # index.html.erb <%= show_link project %> And the setup if defined? ActionController ActionController::Base.class_eval do AuthAssistant::ViewHelpers::AuthLink end end # module AuthAssistant::ViewHelpers::AuthLink def self.included(base) base.helper_method :create_link, :delete_link, :edit_link, :show_link base.helper_method :new_link, :destroy_link, :update_link, :read_link 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.
Michael Koziarski
2010-May-13 21:22 UTC
Re: What is the deal with Controller additions and helper methods!?
> But using link_to from within this context, somehow I don''t have > access to the controller variable used internally, fx in url_for.That''s because the controller itself has its own implementation of url_for, if you attempt to reuse those helpers in the controller it''s just not going to work. Given that link_to is defined in the same module as the view''s url_for, you probably won''t be able to get this working without serious hacks or code duplication. But I''d question what you''re trying to do here, why are your controllers generating html without using views? -- Cheers Koz -- 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.
Ryan Bigg
2010-May-13 21:31 UTC
Re: What is the deal with Controller additions and helper methods!?
On 14 May 2010 07:22, Michael Koziarski <michael@koziarski.com> wrote:> But I''d question what you''re trying to do here, why are your controllers > generating html without using views? >A "sometimes" use-case is putting a link using link_to in the flash. -- Ryan Bigg / Radar -- 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.
Yehuda Katz
2010-May-13 21:33 UTC
Re: What is the deal with Controller additions and helper methods!?
I''d recommend render_to_string :inline for this case. Yehuda Katz Architect | Engine Yard (ph) 718.877.1325 On Thu, May 13, 2010 at 2:31 PM, Ryan Bigg <radarlistener@gmail.com> wrote:> > On 14 May 2010 07:22, Michael Koziarski <michael@koziarski.com> wrote: > >> But I''d question what you''re trying to do here, why are your controllers >> generating html without using views? >> > > A "sometimes" use-case is putting a link using link_to in the flash. > > -- > Ryan Bigg / Radar > > -- > 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<rubyonrails-core%2Bunsubscribe@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.
Kristian Mandrup
2010-May-13 22:51 UTC
Re: What is the deal with Controller additions and helper methods!?
All I actually wanted to do was to add some helper methods for my views. I kind of "solved" the problem by rethinking my approach, as I could see I had gone down a "blind alley". module ApplicationHelper def self.auth_assist_helpers include AuthAssistant::ViewHelpers end end module AuthAssistant module ViewHelpers include AuthAssistant::ViewHelpers::AuthLink include AuthAssistant::ViewHelpers::RestLink include AuthAssistant::ViewHelpers::AuthMenuItem end end -- In my Rails app I can now do the following to have them added. Don''t know how to do a similar feat from my gem, since there is a loading/ timing issue then? module ApplicationHelper auth_assist_helpers end and all the methods are included into ApplicationHelper and hence available for my views, and link_to works as expected from within this context. Not sure if this is the "right" approach however, polluting the ApplicationHelper seems kind of crude. How is the UrlHelper integrated and made available for the views I wonder? I used the previous approach, coz that was what I had seen in code elsewhere, such as in cancan, devise etc. Adding method to ActionController and certain methods made available as helper methods using helper_method :xxxx Thanks!> That''s because the controller itself has its own implementation of > url_for, if you attempt to reuse those helpers in the controller it''s > just not going to work. Given that link_to is defined in the same > module as the view''s url_for, you probably won''t be able to get this > working without serious hacks or code duplication. But I''d question > what you''re trying to do here, why are your controllers generating > html without using views? > > -- > Cheers > > Koz >-- 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.