views:

10

answers:

2

How can I render a show action on certain conditions?

def show
  @post = Post.find_approved
  if @post.approved
      approved = true
  end
  respond_to do |format|
    # I only want to render show.html.erb if approved is true if not I would like to redirect the user back to where he came from
  end
end
A: 

You can do it without intermediate approved variable.

  @post = Post.find_approved
  if @post.approved
    render :action => 'show'
  else
    redirect_to your_url_here
  end
Nikita Rybak
+1  A: 
redirect_to :back unless approved
respond_to do |format|
  render whatever
end
Johnny
both answers work well. thanks
badnaam