4

how do I add a dynamic redirect? I tried it in ApplicationController and ApplicationHelper with no success.

I want something like this:

def dynamic_path
  if current_user.admin?
    admin_path
  else
    overview_path
  end
end

What's the best practice for that?

Thanks!

Edit:

Oh, I forgot to mention that I want to put this into my gem which is used by two different applications and both should use this method. Where exactly should I put it?

Cojones
  • 2,930
  • 4
  • 29
  • 41

2 Answers2

4

Try putting this in the ApplicationController and then add a helper_method line at the top of the application controller like so:

helper_method :dynamic_path

def dynamic_path
  redirect_to (current_user.admin? ? admin_path : overview_path)
end

The helper_method line makes this method available in all your views and controllers.

jvperrin
  • 3,368
  • 1
  • 23
  • 33
  • I added it to the `ApplicationController` of the gem, not working. Any idea why? – Cojones Oct 08 '13 at 16:22
  • @Cojones What kind of gem are you making? I believe only engine style gems have an application controller. Stuart M's solution of making a Railtie would be better than making a full engine if you only need this one helper method to be shared. – jvperrin Oct 09 '13 at 00:41
  • The gem is already there and a full engine, it's got a lot of functionality and since both applications using the gem need this dynamic path I thought I'd be good to have it in the gem as well? I'm still trying to make the Railtie work. – Cojones Oct 09 '13 at 09:18
1

Use redirect_to:

def dynamic_path
  redirect_to (current_user.admin? ? admin_path : overview_path)
end

Update: And since it sounds like you're trying to store this helper module in an external gem you'll need to ensure your module gets loaded as an ActionView helper, which can be done automatically by using a Railtie in your gem. See

How do I extract Rails view helpers into a gem?

Community
  • 1
  • 1
Stuart M
  • 11,458
  • 6
  • 45
  • 59
  • Same here, doesn't seem to be found inside the `ApplicationController` of the gem. – Cojones Oct 08 '13 at 16:23
  • Are you properly `include`'ing your helper module from the gem in `ApplicationController`? For instructions on automatically including a helper module from a Gem into the standard set of ActionView helpers, see http://stackoverflow.com/questions/5791211/how-do-i-extract-rails-view-helpers-into-a-gem – Stuart M Oct 08 '13 at 16:46
  • Updated my answer to mention that too – Stuart M Oct 08 '13 at 16:48
  • I've been playing around with this and even copied it 1:1 only changing the names and it's still not working: `/Users/pbartels/Sites/e_core/lib/e_core/railtie.rb:4:in 'block in ': uninitialized constant ECore::Railtie::RoutesHelper (NameError)` – Cojones Oct 09 '13 at 14:05
  • Can you edit your post to include the relevant Railtie code? Sounds like an issue loading/requiring the file – Stuart M Oct 09 '13 at 15:43
  • I got it working using this solution: http://stackoverflow.com/questions/11124447/how-to-incorporate-rails-engine-applicationcontroller-methods-in-a-main-app - Thanks! – Cojones Oct 10 '13 at 11:29