Abram
Abram

Reputation: 41914

Redirect-related double render error in rails

In my controller I am redirecting the user if they are signed out. Then I am pulling a list of professionals.. and need to redirect there too, if none exist. Is there a way to solve this dilemma?

  def purchase
    @style = Style.find(params[:id])
    if user_signed_in? && current_user.consumer 
      @professionals = Professional.where(...)
      if @professionals.empty?
        redirect_to style_path(@style)
      else
        ...
      end
      ...
    else
      flash[:error] = "Please sign in as a consumer to access this page"
      redirect_to style_path(@style)
    end
  end

Upvotes: 3

Views: 3066

Answers (3)

wmjbyatt
wmjbyatt

Reputation: 696

Similar to the above answers, some just prefer the style of

return redirect_to style_path(@style)

Upvotes: 4

Rails Guy
Rails Guy

Reputation: 3866

Change your following code to this :

  if @professionals.empty?
    redirect_to style_path(@style) and return

Hope it will help. Thanks

Upvotes: 0

vee
vee

Reputation: 38645

Try adding and return so that the action returns and does not continue. Please try the following:

redirect_to style_path(@style) and return

Upvotes: 6

Related Questions