Rails发送到控制器查看信息

时间:2013-11-04 09:58:22

标签: ruby-on-rails ruby

我是Ruby on Rails的新手,我无法理解如何通知或发送查看控制器的一些信息。这是code =>

  def index
    @post = Post.all()

    #here i want notify new_post_view about Post's empty
    if @post.length == 0
      redirect_to new_post_path
    end

  end

我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

试试这个:

def index
  @posts = Post.all
  if @posts.empty?
    flash[:notice] = "No posts found"
    redirect_to(new_post_path) 
  end
end

在您的布局模板中:

<p id="notice"><%= flash[:notice] %></p>
相关问题