当前位置: 动力学知识库 > 问答 > 编程问答 >

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.

网友答案:

You can also make the switch in the controller.

def index
  @articles = Article.all
  render "index_without_articles" if @article.nil?
end
网友答案:

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

网友答案:

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"

分享给朋友:
您可能感兴趣的文章:
随机阅读: