0

I would like to give my application 2 different views (HTML, CSS, JS):

  1. for an unauthorized user (nice looking one)
  2. only for an authorized user (raw tables with all model data and its available actions buttons)

and make possible for the authorized user to switch between them.

As for now I have 2nd and I'm going to create user authorization (probably with CanCan gem) and then 1st.

guides.rubyonrails.org "2.2.12.2 Choosing Layouts at Runtime" describes nice way to switch between layouts but it's not enough for me I think.

In my case both layouts would look the same or almost the same:

<!DOCTYPE html>
<html>
<head>
# layout depended JS and CSS maybe
</head>
<body class="container">

<%= render 'layouts/navbar' %>
<%= render 'layouts/flash' %>

<%= yield %>

</body>
</html>

What I'm interested in is to decide somehow which folder will be used to fill yield above. I have slides_controller.rb and /app/views/slides/* and I would like to create one more, let say /app/views/slides_nice/* and use same slides_controller to decide which one should be used for rendering.

4
  • I have one more idea to create another controller which will inherit from the one I have (for authorized user) and it'll use another folder in /app/views/. I'm not sure, is it good approach ? Commented Mar 21, 2014 at 0:16
  • It's probably not gonna work well with routes, as you're essentially creating two actions to handle the same url. If you are only going to provide different rendering results for only one action, then authenticate on that particular action shall make sense impo. Commented Mar 21, 2014 at 0:33
  • I would duplicate routes (changing url of course) as well and that way I can forget about variable I would have to pass with every request by authorized user to know his choice for the view. Commented Mar 21, 2014 at 0:38
  • that would be rather obtrusive and not so intuitive to me, I'd suggest you try what you want and get a feeling on which solution is simpler and more logical. You are the one knows what you want after all. Commented Mar 21, 2014 at 0:46

1 Answer 1

1

I think what you need is to authenticate in controller and give different template to render.

such as (assuming there is current_user helper from devise or your own authentication solusion):

if current_user 
  render "template_1" 
else 
  render "template_2"

Also CanCan is currently outdated as R Bates no longer updating it. It does not work with new rails releases. And i'm not sure if role based authorization is what you want, you seems just want to hide something from guest users.

Sign up to request clarification or add additional context in comments.

4 Comments

I know it should work but the question is if there any better aproach ? Native for Rails to set /app/views/subfolder on top of the controller. Which makes me a bit more happy is that I can put this to before_action.
Well the logic is you want to control what views to present based on user status. These code shall go to proper controllers, not the views. I'm not quite sure about your concerns on "better approach".
Something like this layout thing. Good to know about CanCan.. quite sad as I watched Railcast about it and it looked really promising.
Can you please read the comment I added to my question and tell me what do you think about it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.