Home>

I'm using an app in Rails, but when I use two renders, I get an error
Please teach me how to do these two processes.

controller

def create
    @tweet = Tweet.create (tweet_params)
    return redirect_to tweets_path if @ tweet.save
    render "new"
    render layout: "application"
  end

The first is a render to redraw the screen if @tweet is not saved
The second is a render to use a different layout file

error What I tried

Write return after the first render
However, when return is used, it seems that the subsequent processing is not executed and the second render is not executed.

def create
    @tweet = Tweet.create (tweet_params)
    return redirect_to tweets_path if @ tweet.save
    render "new" and return
    render layout: "application"
  end

Write render layout earlier

def create
    @tweet = Tweet.create (tweet_params)
    ender layout: "application"
    return redirect_to tweets_path if @ tweet.save
    render "new" and return
  end


Another error occurred

  • Answer # 1

    Is it something like this?

    render "new", layout: "application"

  • Answer # 2

    Please separate the process itself.
    Only one result can be given to one controller. (Same for all languages ​​and frameworks)

    It doesn't seem to be rendered separately depending on the conditions, and please prepare two methods in the controller and execute them in order.