Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering different views in one action

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?

like image 390
Lucy Weatherford Avatar asked Apr 10 '13 10:04

Lucy Weatherford


People also ask

How do you render partials?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What are rails partials?

Ruby on Rails Views Partials Partial templates (partials) are a way of breaking the rendering process into more manageable chunks. Partials allow you to extract pieces of code from your templates to separate files and also reuse them throughout your templates.

How do you use nested layouts rails?

Rails provides us great functionality for managing layouts in a web application. The layouts removes code duplication in view layer. You are able to slice all your application pages to blocks such as header, footer, sidebar, body and etc.

How can you tell Rails to render without a layout *?

By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option.


1 Answers

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`   end end 

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   end end  private  def show_signed_in    # declaring some instance variables to use in the view..    render 'some_view' end  def show_not_signed_in    # declaring some other instance variables to use in the view..    render 'some_other_view' end 
like image 136
Zippie Avatar answered Oct 05 '22 19:10

Zippie