![]() With this code, the browser will make a fresh request for the index page, the code in the index method will run, and all will be well. nil? redirect_to action: :index end end Copy Rails understands both numeric status codes and the corresponding symbols shown below. Render status: 500 render status: :forbidden Copy For example, if you have this code in your BooksController class: By default, controllers in Rails automatically render views with names that correspond to valid routes. Default rendering is an excellent example of this. You've heard that Rails promotes "convention over configuration". Call head to create a response consisting solely of HTTP headers to send back to the browserĢ.1 Rendering by Default: Convention Over Configuration in Action.Call redirect_to to send an HTTP redirect status code to the browser.Call render to create a full response to send back to the browser.2 Creating Responsesįrom the controller's point of view, there are three ways to create an HTTP response: You'll see all of those paths later in this guide. If the response is a full-blown view, Rails also does some extra work to wrap the view in a layout and possibly to pull in partial views. In broad strokes, this involves deciding what should be sent as the response and calling an appropriate method to create that response. It's that handoff that is the subject of this guide. ![]() But then, when it's time to send a response back to the user, the Controller hands things off to the View. As you know, the Controller is responsible for orchestrating the whole process of handling a request in Rails, though it normally hands off any heavy code to the Model. This guide focuses on the interaction between Controller and View in the Model-View-Controller triangle.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |