-1

私はRuby on Railsの初心者で、コントローラーから情報を表示するために通知または送信する方法を理解できません。ここにコードがあります=>

  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

どうすればこれを行うことができますか??

4

1 に答える 1

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>
于 2013-11-04T10:02:50.663 に答える