简体   繁体   中英

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.

I believe you want render_to_string . See this blog post for more info on rendering in Rails 3.

You can also make the switch in the controller.

def index
  @articles = Article.all
  render "index_without_articles" if @article.nil?
end

maybe it's a workaround but it's quite an easy solution

<%= render :partial => "blank_#{@articles.blank?}" %>

and have two partials called "_blank_true.html.erb" and "_blank_false.html.erb"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM