Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render partial in controller using Ruby On Rails

Im using Rails 3 in my project.

In controller > articles In view > index.html.erb

<% if @articles.blank? %>
<%= render :partial => "blank" %>

I dont want to write querysets in views for checkin (if empty do this or do this) How can I pass blank slate partial (if queryset is empty) inside controller ?

Thanks.

like image 603
Harry Avatar asked Jan 11 '11 18:01

Harry


People also ask

What is render partial in Ruby on Rails?

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 I render in Ruby on Rails?

From the controller's point of view, there are three ways to create an HTTP response: Call render to create a full response to send back to the browser. Call redirect_to to send an HTTP redirect status code to the browser. Call head to create a response consisting solely of HTTP headers to send back to the browser.

What is use of partials in Rails?

A partial allows you to separate layout code out into a file which will be reused throughout the layout and/or multiple other layouts. For example, you might have a login form that you want to display on 10 different pages on your site.


1 Answers

You can also make the switch in the controller.

def index
  @articles = Article.all
  render "index_without_articles" if @article.nil?
end
like image 189
Andreas Richter Avatar answered Sep 24 '22 02:09

Andreas Richter