Rendering different views in one action
1.case: your views are going to have similar content, but only the signed in users will have extra options like editing.
You should use a partial view and in your main view you should write something like this:
<% if signed_in? %> <%= render 'edit_form' %><% end %>
Remember, the name of the partial should always start with a underscore, so your partial in this case would be called _edit_form.html.erb
or _edit_form.html.haml
, depending on what you are using.
2.case: depending on if the user is signed in or not, you want to render completely different views, then you should handle it in your controller:
def show if signed_in? render 'show_with_edit' else render 'show_without_edit` endend
And your files would be named show_with_edit.html.erb
and show_without_edit.html.erb
Also, if your view for a signed in user was called show
then you could just do this:
def show render 'show_without_edit' unless signed_in?end
3.case: if you want to change basically EVERYTHING depending if the user is signed in or not, you could create some custom methods and call them inside your original action like this:
def show if singed_in? show_signed_in else show_not_signed_in endendprivatedef show_signed_in # declaring some instance variables to use in the view.. render 'some_view'enddef show_not_signed_in # declaring some other instance variables to use in the view.. render 'some_other_view'end