badnaam
badnaam

Reputation: 1884

rails conditional show

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

Upvotes: 0

Views: 63

Answers (2)

Johnny
Johnny

Reputation: 7341

redirect_to :back unless approved
respond_to do |format|
  render whatever
end

Upvotes: 1

Nikita Rybak
Nikita Rybak

Reputation: 68006

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

Upvotes: 0

Related Questions