I want to have 2 kinds of views for the same posts in my rails application. For instance - in one where a logged in user can update and edit the post, and in the other any user can just view it and comment on it or select it.
How should I go about this? Do I need a separate class? I know I need a separate view for each, but how about the model and the controller?
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:
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:
And your files would be named
show_with_edit.html.erb
andshow_without_edit.html.erb
Also, if your view for a signed in user was called
show
then you could just do this: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: