1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #21738 from cllns/remove-to_s-from-example

[Engines Guide] Remove to_s example, since it's outside the scope
This commit is contained in:
Eileen M. Uchitelle 2015-09-25 12:24:51 -04:00
commit 3c52b2000c

View file

@ -843,28 +843,10 @@ above the "Title" output inside `app/views/blorgh/articles/show.html.erb`:
```html+erb ```html+erb
<p> <p>
<b>Author:</b> <b>Author:</b>
<%= @article.author %> <%= @article.author.name %>
</p> </p>
``` ```
By outputting `@article.author` using the `<%=` tag, the `to_s` method will be
called on the object. By default, this will look quite ugly:
```
#<User:0x00000100ccb3b0>
```
This is undesirable. It would be much better to have the user's name there. To
do this, add a `to_s` method to the `User` class within the application:
```ruby
def to_s
name
end
```
Now instead of the ugly Ruby object output, the author's name will be displayed.
#### Using a Controller Provided by the Application #### Using a Controller Provided by the Application
Because Rails controllers generally share code for things like authentication Because Rails controllers generally share code for things like authentication