Conversation
… the partials subsection in favor of Action View guide
|
Hi @carlosantoniodasilva - I wanted to request your thoughts on the updated structure of Layouts and Rendering guide. It isn't ready for a full line by line review yet (I've make it to line 700 out ~1000) but before I end work for this month I wanted to ask if this looks better than before overall and I'm happy to incorporate any structural feedback early.
|
|
@bhumi1102 sorry I didn't get to this one last week... I think the overall structure looks better, yes. The one thing to consider at a glance, is whether "options for render" warrants its own main section vs staying as a subsection. Other than that, my initial thinking is related to the fact that it still includes content both very related to Action Controller, and Action View, and whether you are thinking about breaking that further somehow? I know they're super tied together, but there's things I'm not sure fit in the same guide... for instance, explaining redirect_to / head versus layouts for partials / nested layouts. Very separate concepts, that are more related to controller or view respectively. The way I've been sort of thinking about it is that we could have the AV Overview, and an "AV Advanced" (i.e. Layouts and Rendering potentially, but from AV's perspective only) guide, and we'd have the current AC Overview, plus an "AC Advanced / AC Rendering & Responses" of some sort, that'd be tying AC + AV: render, redirect, head, selecting the layout, and so on. That's what I think Layouts and Rendering is trying to do, but mixing with some "AV Advanced" content that's very much AV-specific. Maybe it doesn't make sense, and we just keep Layouts and Rendering as an advanced concept that goes through these topics together, but in my mind it seems that distinction/separation/organization could be beneficial. /cc @p8 for any input as well. |
|
Thanks @carlosantoniodasilva, and no worries! I'm only able to work on guides 2 to 3 hours each week for the month of April so I'll be little slow as well. But I'll focus on getting something ready for an internal review is next.
Agree. I think that can go back to subsection.
I read something along those lines in your original TODO task. What I'll plan to do next is come up the main headers + sub-headers for each of these guides (in my 3 hours next week) and see if we can get somewhere better with separation/organization. It's not immediately clear to me what content will go where, so I'll give it shot and see! |
|
@carlosantoniodasilva @bhumi1102 I've always found the focus of this guide confusing. It also doesn't help that we handle Action View before Action Controller, but still reference controllers in the Action View guides. Looking at the "Getting Started" guide, it first generates a model and then a controller (which will create the views). I think having that order makes sense.
|
|
Also, some of the current guide titles don't describe what they help you do. After "Getting Started" (helping you get started), the next one is "Active Record Basics" (what does this do if I don't know what Active Record is?). |
|
We haven't looked into the "Getting Started" guide because that was going to get a full review/rewrite as part of a tutorial work at some point, so we kinda skipped it. I do agree it would make sense to explain Controller before View in the order of things, not necessarily following the "MVC" order there. As for the titles, it could make sense to have them be more descriptive, or at least reworking/improving the description of the guides (which is something worth doing nonetheless I think). Let's maybe move this part of the discussion to Basecamp so others can chime in, @p8 if you want to start a new to-do or discussion there about it. For this guide in particular, what bothers me a bit is that it's under "View", but it's a mixed guide that's mostly about rendering layouts / views from the "Controller"'s perspective, with some view-specific/only stuff. My thinking was to decouple that some, and make it a more complete / advanced guide on rendering stuff from the "View" perspective, leaving another guide to handle the "Controller->View" portion. I hope that makes sense. |
| end | ||
| ``` | ||
|
|
||
| But we do not need write the above boilerplate `index` action. You'd define the `index` method once you need to do anything other than `render :index`. If you create a `Book` model and add the following index action to `BooksController`: |
There was a problem hiding this comment.
| But we do not need write the above boilerplate `index` action. You'd define the `index` method once you need to do anything other than `render :index`. If you create a `Book` model and add the following index action to `BooksController`: | |
| But we do not need to write the above boilerplate `index` action. You'd define the `index` method once you need to do anything other than `render :index`. If you create a `Book` model and add the following index action to `BooksController`: |
| Creating Responses Using `render` | ||
| --------------------------------- | ||
|
|
||
| This section describes the various way in which you can customize the behavior of `render`. The controller's [`render`][controller.render] method does the heavy lifting of constructing a response to HTTP requests and sending your application's content to the client. |
There was a problem hiding this comment.
| This section describes the various way in which you can customize the behavior of `render`. The controller's [`render`][controller.render] method does the heavy lifting of constructing a response to HTTP requests and sending your application's content to the client. | |
| This section describes the various ways in which you can customize the behavior of `render`. The controller's [`render`][controller.render] method does the heavy lifting of constructing a response to HTTP requests and sending your application's content to the client. |
| * Call [`render`][controller.render] to create a full response to send back to the browser. More examples in [Creating Responses Using `render` section](#creating-responses-using-render). | ||
| * Call [`redirect_to`][] to send an HTTP redirect status code to the browser. More examples in [Creating Responses Using `redirect-to` section](#creating-responses-using-redirect-to) | ||
| * Call [`head`][] to create an HTTP header only response. More in [this section](#building-header-only-responses-using-head). |
There was a problem hiding this comment.
A fourth options is streaming data to the browser:
https://edgeapi.rubyonrails.org/classes/ActionController/DataStreaming.html
| NOTE: Unless overridden, your response returned from this render option will be | ||
| `text/plain`, as that is the default content type of Action Dispatch response. |
There was a problem hiding this comment.
Ignore this if it's too much of a nitpick 😄
If we reference Action Dispatch Response should we capitalize response?
| NOTE: Unless overridden, your response returned from this render option will be | |
| `text/plain`, as that is the default content type of Action Dispatch response. | |
| NOTE: Unless overridden, your response returned from this render option will be | |
| `text/plain`, as that is the default content type of Action Dispatch Response. |
If instead it references a response should we add "an"?
| NOTE: Unless overridden, your response returned from this render option will be | |
| `text/plain`, as that is the default content type of Action Dispatch response. | |
| NOTE: Unless overridden, your response returned from this render option will be | |
| `text/plain`, as that is the default content type of an Action Dispatch response. |
|
|
||
| With most of the options to `render`, the rendered content is displayed as part of the current layout. This guide covers more about [finding](#) and [using layouts](#) in the sections below. | ||
|
|
||
| If you need to use a layout other than the current layout Rails uses by default, you can use the `:layout` option to specify a file to use as the layout for the current action: |
There was a problem hiding this comment.
This could sentence is pretty long and a bit hard to parse. Maybe we can split it up a bit?
We also specify a template instead of a file I think.
Maybe:
| If you need to use a layout other than the current layout Rails uses by default, you can use the `:layout` option to specify a file to use as the layout for the current action: | |
| If you need to use a layout, other than the current layout Rails uses by default, you can use the `:layout` option to specify a template to use as the layout for the current action: |
Or maybe something like:
| If you need to use a layout other than the current layout Rails uses by default, you can use the `:layout` option to specify a file to use as the layout for the current action: | |
| If you need to use a layout other than the default layout, you can use the `:layout` option to specify a template to use as the layout for the current action: |
| redirect_back(fallback_location: root_path) | ||
| ``` | ||
|
|
||
| NOTE: `redirect_to` and `redirect_back` do not immediately stop the execution of the controller method, they simply set HTTP responses. So statements *after* `redirect_to` in our controller get executed. To terminate the execution of the method immediately after the `redirect_to`, you need to use `return`. For example `redirect_to book_url(@book) and return` |
There was a problem hiding this comment.
| NOTE: `redirect_to` and `redirect_back` do not immediately stop the execution of the controller method, they simply set HTTP responses. So statements *after* `redirect_to` in our controller get executed. To terminate the execution of the method immediately after the `redirect_to`, you need to use `return`. For example `redirect_to book_url(@book) and return` | |
| NOTE: `redirect_to` and `redirect_back_or_to` do not immediately stop the execution of the controller method, they simply set HTTP responses. So statements *after* `redirect_to` in our controller get executed. To terminate the execution of the method immediately after the `redirect_to`, you need to use `return`. For example `redirect_to book_url(@book) and return` |
|
|
||
| NOTE: `redirect_to` and `redirect_back` do not immediately stop the execution of the controller method, they simply set HTTP responses. So statements *after* `redirect_to` in our controller get executed. To terminate the execution of the method immediately after the `redirect_to`, you need to use `return`. For example `redirect_to book_url(@book) and return` | ||
|
|
||
| [`redirect_back`]: https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_back |
There was a problem hiding this comment.
| [`redirect_back`]: https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_back | |
| [`redirect_back_or_to`]: https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_back_or_to |
|
|
||
| The `render` call will find the `index.html.erb` file and attempt to render it. However, the `index` view expects a `@books` instance variable to be set. Since we are coming from the `show` action, while trying to render the `index` view, the `@books` instance variable will *not* be set. Note that `render action: "index"` does *not* execute the code in the `index` controller action before rendering the view, it just attempts to render the view specified. | ||
|
|
||
| This is the difference between `render` and `redirect_to`. The `redirect_to` method ends the current action, the browser makes a brand new HTTP request, which in turn, execute the appropriate controller action. |
There was a problem hiding this comment.
| This is the difference between `render` and `redirect_to`. The `redirect_to` method ends the current action, the browser makes a brand new HTTP request, which in turn, execute the appropriate controller action. | |
| This is the difference between `render` and `redirect_to`. The `redirect_to` method ends the current action, the browser makes a brand new HTTP request, which in turn, executes the appropriate controller action. |
|
|
||
| Just like the `:status` option for `render`, `:status` for `redirect_to` accepts both numeric and symbolic values. | ||
|
|
||
| The Difference Between `render` and `redirect_to` |
There was a problem hiding this comment.
This section seems to describe when to use render and when to use redirect_to.
Maybe the following header would be clearer? This would also mean that line 558 should change.
| The Difference Between `render` and `redirect_to` | |
| When to use `render` or `redirect_to` |
Or maybe:
| The Difference Between `render` and `redirect_to` | |
| Choosing between `render` and `redirect_to` |
|
|
||
| With this code, the browser will make a new request for the index page, the code in the `index` method will run, which will set the `@books` instance variable, then the `index.html.erb` view will be rendered. And it will all work as expected. | ||
|
|
||
| Note that the above example is for demonstration. It is not typical to render all books from a `show` action for a given book. If a resource is not found, it is reasonable to return a HTTP 404 Not Found status code when `@book.nil` is true. |
There was a problem hiding this comment.
If the example is not typical, should we use a more typical example like a create action instead?
This example could also explain that a redirect prevents a double submit when using the back button after a form is submitted?




-> Removed the partial duplication and restructured quite a bit to separate out the sections
-> code examples of few options help here
-> Updated to books.