Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you need this across multiple controllers then just create a new method in application_controller.rb</p> <p>You could define a method like this in your application_controller.rb</p> <pre><code>def user_type?(user) return user.user_type.to_s end </code></pre> <p>and switch your controller methods to use this as such</p> <pre><code>def index render user_type?(current_user) end </code></pre> <p>But for that matter; if your use case is that simple, why not just do this directly in your controller methods like this?</p> <pre><code>def index render current_user.user_type.to_s end </code></pre> <hr> <p>Edit to answer followup question</p> <hr> <p>If you take a look at the Rails Guide <a href="http://guides.rubyonrails.org/layouts_and_rendering.html" rel="nofollow">http://guides.rubyonrails.org/layouts_and_rendering.html</a> the section "Choosing Layouts at Runtime" they are pretty much handling this in the same way outlined here. </p> <p>If the different user types are seeing 2 truly different pages then this is the way to go; keep the logic in the controller or application_controller. </p> <p>If, however, both of your user types are seeing essentially the same thing, but with small differences, or a specific section of the page, then you might want to always render the same view no matter the user type. You could then check then compose the view of multiple partials to bring in the correct pieces dependent on the user. Your "logic code" should be kept out of the view as much as possible, so in this case you would want to couple this with helper methods located at /app/helpers. Methods defined within /app/helpers/application_helper.rb will be available to all views. You can also create controller specific view helper methods within /app/helpers/controller_name_helper.rb</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload