1

I am trying to conditionally route two different controller actions. I have created RoutesController#root and sent root there from routes.rb, however the app just wants to find a root template to render no matter what i write in the root method.

What I am trying to achieve is:

  1. user requests ‘/’
  2. user gets pushed to sign in
  3. upon successful sign in if current_user.company.present? (current_user should be available in the Routes controller right?) then render Quotes#new
  4. else if no company then render Companies#new

I'm hitting a missing template error;

Missing template companies/1/quotes/new.1 with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "app/views"

I want it to be searching in app/views/quotes/new, what am i doing wrong?

RoutesController.rb

class RoutesController < ActionController::Base
  before_filter :authenticate_user!

  def root
    if current_user.company.present? 
      render new_company_quote_path(current_user)# 'quotes#new'
    else
      render new_company_path(current_user) # 'companies#new'
    end
  end
end

routes.rb

root 'routes#root'
jbk
  • 1,911
  • 19
  • 36
  • 1
    have you tried giving your controller and action a different name? as they seem to be reserved keywords.. – Md. Farhan Memon Jul 17 '17 at 09:47
  • try to put `exit` in root method. than you can know it actually going to root method or not . – Vishal Jul 17 '17 at 09:50
  • Was a simple file naming error at my end :/. It's now using RoutesController#root just fine, but is now hitting a 'Template is missing' error, as above in question body. – jbk Jul 17 '17 at 09:59
  • 1
    use `redirect_to` instead – Md. Farhan Memon Jul 17 '17 at 10:01
  • 1
    It should be redirect_to not render. – Vishal Jul 17 '17 at 10:03
  • amazing, , thanks Farhan & Vishal. Farhan just pipped you to the post Vishal, so Farhan pls post last comment as an answer with why redirect_to not render and I'll accept as well as re-titling the questoin. Also guys, is what I'm doing ok practice according to rails lore do you think? – jbk Jul 17 '17 at 10:05
  • Also @md-farhan-memon would be great if you'd specify why redirect_to works and render does not in the answer for the sakes of good content for others. Thanks – jbk Jul 17 '17 at 10:13
  • @jbk Can i post answer ? will you accept it :) – Vishal Jul 17 '17 at 10:16
  • 2
    ha :) Farhan just pipped you to the post so I guess fairness'd say I should accept his answer, but he seems to have gone offline so lets give him 10mins, if he doesn't answer then no probs I'll accept yours . Pipped you again Vishal, ;), next time and thanks for both of yoru help. – jbk Jul 17 '17 at 10:18

1 Answers1

1

render is used when you just want to render/display a particular view from the path, it doesn't execute any code. For detailed differences, see this post.

Thus, in your case, it should be redirect_to instead of render.

And regarding the best practice thing, it looks good to me.

Md. Farhan Memon
  • 6,055
  • 2
  • 11
  • 36