Skip to content

Commit 26e4653

Browse files
authored
Ruby on Rails: Address lint issues in the Rails Basics section (#30474)
* fix: address lint issues in controllers.md * fix: address lint issues in routing.md * fix: address lint issues in views.md * fix: edit the heading in controllers.md * fix: fix formatting of a note box in routing.md * fix: fix minor formatting in views.md
1 parent ab816d8 commit 26e4653

File tree

3 files changed

+62
-55
lines changed

3 files changed

+62
-55
lines changed

ruby_on_rails/rails_basics/controllers.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ The important distinction between the "scalar" parameter values like strings and
116116

117117
<div class="lesson-note" markdown="1">
118118

119-
This used to be done in Rails 3 by setting `attr_accessible` in the model to allow attributes, so you will probably see that in a lot of Stack Overflow posts and earlier applications.
119+
#### Whitelisting "safe" attributes in Rails 3
120+
121+
This used to be done in Rails 3 by setting `attr_accessible` in the model to allow attributes, so you will probably see that in a lot of Stack Overflow posts and earlier applications.
120122

121123
</div>
122124

@@ -164,15 +166,17 @@ So our `#create` action above can now be filled out a bit more:
164166

165167
<div class="lesson-note lesson-note--warning" markdown="1">
166168

167-
Prior to Rails 8, strong parameters were handled differently. Instead of the `#expect` method, you had to call `#require` on the top level key name followed by calling `#permit` on the list of attributes. For example:
169+
#### Handling strong parameters before Rails 8
168170

169-
```ruby
170-
def allowed_post_params
171-
params.require(:post).permit(:title, :body, :author_id)
172-
end
173-
```
171+
Prior to Rails 8, strong parameters were handled differently. Instead of the `#expect` method, you had to call `#require` on the top level key name followed by calling `#permit` on the list of attributes. For example:
172+
173+
```ruby
174+
def allowed_post_params
175+
params.require(:post).permit(:title, :body, :author_id)
176+
end
177+
```
174178

175-
This way still works, but it has a couple of security flaws that motivated the development of `#expect`. But you will likely be exposed to this way of doing it through older projects, blog posts, and StackOverflow answers. Just know that this is serving the same function as the new `#expect` method.
179+
This way still works, but it has a couple of security flaws that motivated the development of `#expect`. But you will likely be exposed to this way of doing it through older projects, blog posts, and StackOverflow answers. Just know that this is serving the same function as the new `#expect` method.
176180

177181
</div>
178182

ruby_on_rails/rails_basics/routing.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,13 @@ You should have a good sense of what's going on in the routes file by now but pr
204204
1. Read the [Rails Guides chapter on Routing](http://guides.rubyonrails.org/routing.html), sections 1-2.5, 3.1-3.4, 4.6, and 6.1
205205
1. Watch this [Wonderful explanation of how REST and HTTP works](https://www.youtube.com/watch?v=Q-BpqyOT3a8). You can follow the tutorial using `curl https://api.github.com`.
206206

207-
<div class="lesson-note lesson-note--tip" markdown="1">
207+
<div class="lesson-note lesson-note--tip" markdown="1">
208208

209-
The Postman Chrome Extension shown in the video is deprecated. Use the native [Postman App](https://www.postman.com/downloads/) or use the web version.
209+
#### Don't use the Postman Chrome Extension
210210

211-
</div>
211+
The Postman Chrome Extension shown in the video is deprecated. Use the native [Postman App](https://www.postman.com/downloads/) or use the web version.
212+
213+
</div>
212214

213215
</div>
214216

ruby_on_rails/rails_basics/views.md

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ So if a layout is basically just a shell around the individual page, how does th
3131

3232
The other thing you've undoubtedly noticed is the odd HTML code that goes inside `<%=` and `%>` tags. This is Embedded Ruby (ERB). It's a special way of executing ruby code inside your HTML. HTML is static, so you need to dial in some Ruby if you want to do anything dynamic like looping, `if` statements or working with variables. ERB (and another similar language you might see called HAML) do exactly that.
3333

34-
What those tags do is execute whatever you see inside them exactly as if it was normal Ruby. So `<em><%= "I am emphasized" %></em>` will output an emphasized piece of text like <em>I am emphasized</em> and `<%= @user.first_name %>` might output `joe`.
34+
What those tags do is execute whatever you see inside them exactly as if it was normal Ruby. So `<em><%= "I am emphasized" %></em>` will output an emphasized piece of text like *I am emphasized* and `<%= @user.first_name %>` might output `joe`.
3535

3636
The difference between `<%` and `<%=` is that the `<%=` version actually displays whatever is returned inside the ERB tags. If you use `<%`, it will execute the code but, no matter what is returned by that line, it will not actually display anything in your HTML template. `<%#` is used to comment and will not execute.
3737

3838
Most of your tags will be `<%=` because you'll find yourself often just outputting important pieces of the instance variables that you received from your controller, like in the `<%= @user.first_name %>` example above. You use the `<%` for purely code-related stuff like `if` statements and `each` loops, where you don't actually WANT anything displayed (the stuff you display will occur inside the loop).
3939

4040
If this is confusing, here's an example: Say we want to display the first names of all the users in our application, but only if the current user is signed in. This might look something like:
4141

42-
~~~erb
42+
```erb
4343
<% if current_user.signed_in? %>
4444
<ul>
4545
<% @users.each do |user| %>
@@ -49,25 +49,25 @@ If this is confusing, here's an example: Say we want to display the first names
4949
<% else %>
5050
<strong>You must sign in!</strong>
5151
<% end %>
52-
~~~
52+
```
5353

5454
Remember to close your statements and loops with `<% end %>`! (You'll forget a few times.)
5555

5656
In the code above, if the user is signed in it will actually render to the web something like:
5757

58-
~~~html
58+
```html
5959
<ul>
6060
<li>Bob</li>
6161
<li>Joe</li>
6262
<li>Nancy</li>
6363
</ul>
64-
~~~
64+
```
6565

6666
If the user isn't signed in, it'll be the much shorter:
6767

68-
~~~html
68+
```html
6969
<strong>You must sign in!</strong>
70-
~~~
70+
```
7171

7272
In the above code, if we had accidentally used `<%=` in the loop line, e.g. `<%= @users.each do |user| %>` it would run the code fine, but because `each` returns the original collection, we'd also see a dump of our `@users` variable on our page (not very professional). It'll happen to you several times and you'll learn quick.
7373

@@ -87,12 +87,12 @@ Another nice thing you can do in Rails is break apart your views into partials.
8787

8888
Pulling back a bit, partials are just HTML files that aren't meant to be complete but can be shared by other files. You would call a partial by writing something like:
8989

90-
~~~erb
90+
```erb
9191
# app/views/users/new.html.erb
9292
<div class="new-user-form">
9393
<%= render "user_form" %>
9494
</div>
95-
~~~
95+
```
9696

9797
There are a couple of syntax oddities you need to pay attention to. The view partial file is named with an underscore like `_user_form.html.erb` but gets called using just the core portion of the name, e.g. `user_form` in the example above.
9898

@@ -104,136 +104,137 @@ There's a lot you can do with partials and we won't dive into it all here, but o
104104

105105
In the example above, you most likely want to pass the `@user` variable to the partial so your code can render the right kind of form. `render` is just a regular method and it lets you pass it an [options hash](https://stackoverflow.com/questions/18407618/what-are-options-hashes). One of those options is the `:locals` key, which will contain the variables you want to pass. Your code might change to look like:
106106

107-
~~~erb
107+
```erb
108108
<%= render partial: "shared/your_partial", :locals => { :user => user } %>
109-
~~~
109+
```
110110

111-
To use the variable in your partial file, you drop the `@` and call it like a normal variable. Note that you should use the `:locals` option if you're calling the `render` method with a `:partial` key.
111+
To use the variable in your partial file, you drop the `@` and call it like a normal variable. Note that you should use the `:locals` option if you're calling the `render` method with a `:partial` key.
112112

113113
There is a `render` shortcut that allows you to pass in variables without the need of using the `:locals` option:
114114

115-
~~~erb
115+
```erb
116116
<%= render "shared/your_partial", :user => user %>
117-
~~~
118-
117+
```
119118

120119
### Implicit partials
121120

122121
As usual, there are some things you would end up doing so many times that Rails has given you a shortcut. One of these is the act of rendering a model object like a User or a Post. If you want a list of all your users, you could write out the HTML and ERB code for displaying a single user's first name, last name, email etc. many times directly in your `app/views/users/index.html.erb` file or you could keep that code in some sort of `each` loop.
123122

124123
But it's usually best to make the User into its own partial called `_user.html.erb` so you can reuse it in other cases as well. The basic way of calling this might be something just like we saw above, which looks like:
125124

126-
~~~erb
125+
```erb
127126
# app/views/index.html.erb
128127
<h1>Users</h1>
129128
<ul>
130129
<% @users.each do |user| %>
131130
<%= render "user", :locals => {:user => user} %>
132131
<% end %>
133132
</ul>
134-
~~~
133+
```
135134

136135
And in your partial:
137136

138-
~~~erb
137+
```erb
139138
# app/views/_user.html.erb
140139
<li><%= "#{user.first_name} #{user.last_name}, #{user.email}" %></li>
141-
~~~
140+
```
142141

143142
It may seem strange to have only one line in a partial, but trust me that it usually doesn't stay that way for long so it's worth getting the hang of.
144143

145144
So if that's the basic way, what's the magical Rails way? Just tell it to render the User object directly, e.g.:
146145

147-
~~~erb
146+
```erb
148147
# app/views/index.html.erb
149148
<h1>Users</h1>
150149
<ul>
151150
<% @users.each do |user| %>
152151
<%= render user %> <!-- Lots less code -->
153152
<% end %>
154153
</ul>
155-
~~~
154+
```
156155

157156
Rails then looks for the `_user.html.erb` file in the current directory and passes it the `user` variable automatically.
158157

159158
What if you want to render a whole bunch of users like we just did? Rails also does that for you the same way, saving you the trouble of writing out your own `each` loop like we did above. write:
160159

161-
~~~erb
160+
```erb
162161
# app/views/index.html.erb
163162
<h1>Users</h1>
164163
<ul>
165164
<%= render @users %>
166165
</ul>
167-
~~~
166+
```
168167

169168
In that situation, Rails not only finds the `_user.html.erb` file and passes it the correct `user` variable to use, it also loops over all the users in your `@user` collection for you. Pretty handy.
170169

171170
### Helper methods
172171

173172
`render`ing partials isn't the only method you can call from within a view. Rails has a bunch of really handy helper methods that are available for you to use in the view. A few of the most common:
174173

175-
#### `#link_to`
174+
#### #link_to
176175

177176
`link_to` creates an anchor tag URL. Instead of writing:
178177

179-
~~~erb
178+
```erb
180179
<a href="<%= users_path %>">See All Users</a>
181-
~~~
180+
```
182181

183182
You write:
184183

185-
~~~erb
184+
```erb
186185
<%= link_to "See All Users", users_path %>
187-
~~~
186+
```
188187

189188
It's the Rails way. And recall that `users_path` generates a relative URL like `/users` whereas `users_url` generates a full URL like `http://www.yourapp.com/users`. In most cases, it isn't an important distinction because your browser can handle both, but make sure you understand the difference.
190189

191190
### Asset tags
192191

193192
As you may have seen in the application layout file we talked about above, Rails gives you helper methods that output HTML tags to grab CSS or JavaScript files. You can also grab images. These are called Asset Tags. We'll get into the "Asset Pipeline" a bit later, but basically these tags locate those files for you based on their name and render the proper HTML tag.
194193

195-
~~~erb
194+
```erb
196195
<%= stylesheet_link_tag "your_stylesheet" %>
197196
<%= javascript_include_tag "your_javascript" %>
198197
<%= image_tag "happy_cat.jpg" %>
199-
~~~
198+
```
200199

201200
Will render something like:
202201

203-
~~~html
202+
```html
204203
<link href="/assets/your_stylesheet.css" media="all" rel="stylesheet">
205204
<script src="/assets/your_javascript.js"></script>
206205
<img src="/assets/happy_cat.jpg">
207-
~~~
206+
```
208207

209208
Note: in production, your stylesheet and JavaScript will all get mashed into one strangely-named file, so don't be alarmed if it's named something like `/assets/application-485ea683b962efeaa58dd8e32925dadf`
210209

211210
### Forms
212211

213212
Rails offers several different helpers that help you create forms, and we'll go over those in-depth in upcoming lessons.
214213

214+
### Conclusion
215+
216+
Views in general make up the user-facing side of your app. It can be a bit tricky at first to imagine how you choose which view to render, what to include in that view and how to use partials, but a few iterations of working with Rails will show you the conventions pretty quickly. Views will become second nature to you.
217+
215218
### Assignment
216219

217220
Now that you've got a taste of the high-level stuff, read through the Rails Guides for a more detailed look at things. The chapter below will actually start in the controller, where you need to let it know WHICH view file you want to render. The second half of the chapter gets more into the view side of things.
218221

219222
<div class="lesson-content__panel" markdown="1">
220223

221-
1. Read the [Rails Guide chapter on Layouts and Rendering](http://guides.rubyonrails.org/layouts_and_rendering.html), sections 1 through 3.4. You can certainly skim when they start going over all the many different options you can pass to a given function... it's good to know what they are and where you can find them, but you don't need to memorize all of them. Usually you'll have something that you want to do, Google it, and find a Stack Overflow post that shows you the option you can use.
224+
1. Read the [Rails Guide chapter on Layouts and Rendering](http://guides.rubyonrails.org/layouts_and_rendering.html), sections 1 through 3.4. You can certainly skim when they start going over all the many different options you can pass to a given function... it's good to know what they are and where you can find them, but you don't need to memorize all of them. Usually you'll have something that you want to do, Google it, and find a Stack Overflow post that shows you the option you can use.
225+
222226
</div>
223227

224-
### Conclusion
228+
### Knowledge check
225229

226-
Views in general make up the user-facing side of your app. It can be a bit tricky at first to imagine how you choose which view to render, what to include in that view and how to use partials, but a few iterations of working with Rails will show you the conventions pretty quickly. Views will become second nature to you.
230+
The following questions are an opportunity to reflect on key topics in this lesson. If you can't answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.
227231

228-
### Knowledge check
229-
This section contains questions for you to check your understanding of this lesson. If you’re having trouble answering the questions below on your own, review the material above to find the answer.
230-
231-
* [How do you make sure a preprocessor runs on your view file?](#how-do-preprocessors-work)
232-
* [What is the difference between `<%`, `<%=` and `<%#`?](#preprocessors)
233-
* [What does including `<%= yield %>` in a layout do?](https://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield)
234-
* [Why do we use partials?](https://guides.rubyonrails.org/layouts_and_rendering.html#using-partials)
235-
* [What is the shortcut for rendering a collection as a series of partials?](https://guides.rubyonrails.org/layouts_and_rendering.html#rendering-collections)
236-
* [How do you dynamically link to another page of your Rails app?](#linkto)
232+
- [How do you make sure a preprocessor runs on your view file?](#how-do-preprocessors-work)
233+
- [What is the difference between `<%`, `<%=` and `<%#`?](#preprocessors)
234+
- [What does including `<%= yield %>` in a layout do?](https://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield)
235+
- [Why do we use partials?](https://guides.rubyonrails.org/layouts_and_rendering.html#using-partials)
236+
- [What is the shortcut for rendering a collection as a series of partials?](https://guides.rubyonrails.org/layouts_and_rendering.html#rendering-collections)
237+
- [How do you dynamically link to another page of your Rails app?](#linkto)
237238

238239
### Additional resources
239240

0 commit comments

Comments
 (0)