Skip to content

Commit

Permalink
Update other strong param examples throughout curriculum
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshDevHub committed Nov 25, 2024
1 parent a452344 commit 57c23cd
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions ruby_on_rails/forms_and_authentication/form_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Each one of these inputs is structured slightly differently, but there are some

Will result in your `params` hash containing a key called `description` that you can access as normal, e.g. `params[:description]`, inside your controller. That's also why some inputs like radio buttons (where `type="radio"`) use the `name` attribute to know which radio buttons should be grouped together such that clicking one of them will unclick the others. The `name` attribute is surprisingly important!

Now another thing we talked about in the controller section was nesting data. You'll often want to tuck submitted data neatly into a hash instead of keeping them all at the top level. This can be useful because, as we saw with controllers, it lets you do a one-line `#create` (once you've allowed the parameters with `#require` and `#permit`). When you access `params[:user]`, it's actually a hash containing all the user's attributes, for instance `{first_name: "foo", last_name: "bar", email: "[email protected]"}`. How do you get your forms to submit parameters like this? It's easy!
Now another thing we talked about in the controller section was nesting data. You'll often want to tuck submitted data neatly into a hash instead of keeping them all at the top level. This can be useful because, as we saw with controllers, it lets you do a one-line `#create` (once you've allowed the parameters with `#expect`). When you access `params[:user]`, it's actually a hash containing all the user's attributes, for instance `{first_name: "foo", last_name: "bar", email: "[email protected]"}`. How do you get your forms to submit parameters like this? It's easy!

It all comes back to the `name` attribute of your form inputs. Just use hard brackets to nest data like so:

Expand All @@ -102,7 +102,7 @@ Those inputs will now get transformed into a nested hash under the `:user` key.

Specific parameters of the `params` hash are accessed like any other nested hash `params[:user][:email]`.

Don't forget that you have to allow the params now in your controller using `require` and `permit` because they are a hash instead of just a flat string. See the Controller section below for a refresher on the controller side of things.
Don't forget that you have to allow the params now in your controller using `#expect` because they are a hash instead of just a flat string. See the Controller section below for a refresher on the controller side of things.

This is cool stuff that you'll get a chance to play with in the project.

Expand Down Expand Up @@ -244,7 +244,7 @@ Just as a refresher, here's a very basic controller setup for handling `#new` ac
end

def user_params
params.require(:user).permit(:first_name, :last_name, :other_stuff)
params.expect(user: [:first_name, :last_name, :other_stuff])
end
...
```
Expand Down
2 changes: 1 addition & 1 deletion ruby_on_rails/forms_and_authentication/project_forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ That looks a whole lot like what you normally see when Rails does it, right?

1. You'll get some errors because now your controller will need to change. But recall that we're no longer allowed to just directly call `params[:user]` because that would return a hash and Rails' security features prevent us from doing that without first validating it.
1. Go into your controller and comment out the line in your `#create` action where you instantiated a `::new` User (we'll use it later).
1. Implement a private method at the bottom called `user_params` which will `permit` and `require` the proper fields (see the [Controllers Lesson](/lessons/ruby-on-rails-controllers) for a refresher).
1. Implement a private method at the bottom called `user_params` which will `expect` the proper fields (see the [Controllers Lesson](/lessons/ruby-on-rails-controllers) for a refresher).
1. Add a new `::new` User line which makes use of that new allow params method.
1. Submit your form now. It should work marvelously (once you debug your typos)!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ end
private

def message_params
params.require(:message).permit(:body)
params.expect(message: [:body])
end
```

Expand Down
2 changes: 1 addition & 1 deletion ruby_on_rails/rails_sprinkles/turbo.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class PostsController < ApplicationController
private

def post_params
params.require(:post).permit(:body)
params.expect(post: [:body])
end
end
```
Expand Down

0 comments on commit 57c23cd

Please sign in to comment.