From 3ae1af3b2e52914012ce23e6c0074daee37bfc61 Mon Sep 17 00:00:00 2001 From: Della Maret Date: Mon, 8 Jul 2024 10:57:54 -0400 Subject: [PATCH 1/7] fixed links in advanced forms & project:flight booker lessons --- .../forms_advanced.md | 14 +++++++------- .../project_flight_booker.md | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ruby_on_rails/advanced_forms_and_activerecord/forms_advanced.md b/ruby_on_rails/advanced_forms_and_activerecord/forms_advanced.md index 104bbcedf19..510c5c01d2a 100644 --- a/ruby_on_rails/advanced_forms_and_activerecord/forms_advanced.md +++ b/ruby_on_rails/advanced_forms_and_activerecord/forms_advanced.md @@ -145,7 +145,7 @@ You can also have your form destroy nested forms by first setting the `:allow_de ### Many-to-many relationships -If you've got a `has_many :through` relationship, you'll likely need to go one additional step further by specifying that each side of your relationship is the inverse of the other. It's detailed in [this blog post from ThoughtBot](http://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through). +If you’ve got a `has_many :through` relationship, you’ll likely need to go one additional step further by [specifying that each side of your relationship is the inverse of the other](http://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through). ### Designing your own forms @@ -159,7 +159,7 @@ Don't get discouraged if you get some real head-scratcher moments when building `simple_form` is a gem by Platformatec which can really make your life easier (if you aren't doing anything too crazy). It provides lots of user-friendly features for building forms and is in wide use today. -It's up to you to check out [the documentation](https://github.com/plataformatec/simple_form) and start using it in your own applications as desired. +It's up to you to check out [the documentation for `simple_form`](https://github.com/plataformatec/simple_form) and start using it in your own applications as desired. ### Miscellania: blank submissions that mean delete @@ -172,10 +172,10 @@ Sometimes Rails helper methods will do it for you, but make sure you know what y ### Assignment
- 1. Read the [Rails Guide on Forms](https://guides.rubyonrails.org/form_helpers.html#choices-from-a-collection-of-arbitrary-objects) section 5, which covers populating a form with a collection of objects. - 2. Read the [Same Rails Guide on Forms](http://guides.rubyonrails.org/form_helpers.html#building-complex-forms) section 10, which covers accepting nested form data. - 3. Read the [Same Rails Guide on Forms](http://guides.rubyonrails.org/form_helpers.html#understanding-parameter-naming-conventions) section 8, which covers the parameter conventions for nested forms. - 4. Read [this blog post from Peter Rhoades](https://www.createdbypete.com/2014/04/04/working-with-nested-forms-and-a-many-to-many-association-in-rails-4.html) on working with nested forms. The example covers a lot of the things we've gone over so far, so follow along. Also note how he does the allowing of nested attributes in Rails 4. + 1. Read the Rails Guide on Forms section 5, which covers [populating a form with a collection of objects](https://guides.rubyonrails.org/form_helpers.html#choices-from-a-collection-of-arbitrary-objects). + 2. Read the Same Rails Guide on Forms section 10, which covers [accepting nested form data](http://guides.rubyonrails.org/form_helpers.html#building-complex-forms). + 3. Read the Same Rails Guide on Forms section 8, which covers the [parameter conventions for nested forms](http://guides.rubyonrails.org/form_helpers.html#understanding-parameter-naming-conventions). + 4. Read this blog post from [Peter Rhoades on working with nested forms](https://www.createdbypete.com/2014/04/04/working-with-nested-forms-and-a-many-to-many-association-in-rails-4.html).The example covers a lot of the things we’ve gone over so far, so follow along. Also note how he does the allowing of nested attributes in Rails 4.
### Conclusion @@ -202,7 +202,7 @@ This section contains helpful links to related content. It isn’t required, so - [Simple Form Documentation on GitHub](https://github.com/plataformatec/simple_form) - [`accepts_nested_attributes_for` documentation](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html) -- [Another example of a nested form on SO](http://stackoverflow.com/questions/15648396/rails-how-to-manage-nested-attributes-without-using-accepts-nested-attributes?rq=1) +- [Another example of a nested form on Stack Overflow](http://stackoverflow.com/questions/15648396/rails-how-to-manage-nested-attributes-without-using-accepts-nested-attributes?rq=1) - [Using `inverse_of` to make `accepts_nested_attributes_for` work for `has_many :through` relationships](http://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through) - [Understanding Rails' form authenticity tokens](http://stackoverflow.com/questions/941594/understand-rails-authenticity-token) - [Why not to hardcode your application's secret token in production](http://daniel.fone.net.nz/blog/2013/05/20/a-better-way-to-manage-the-rails-secret-token/) diff --git a/ruby_on_rails/advanced_forms_and_activerecord/project_flight_booker.md b/ruby_on_rails/advanced_forms_and_activerecord/project_flight_booker.md index b5331689679..58e1557a3e6 100644 --- a/ruby_on_rails/advanced_forms_and_activerecord/project_flight_booker.md +++ b/ruby_on_rails/advanced_forms_and_activerecord/project_flight_booker.md @@ -9,7 +9,7 @@ A typical airline booking flow: 1. Enter passenger information for all passengers 1. Enter billing information -Step 4 would be done via integration of something like [Paypal](http://coding.smashingmagazine.com/2011/09/05/getting-started-with-the-paypal-api/), via [a gem](https://github.com/nov/paypal-express) or [an SDK](https://www.tommyblue.it/2013/07/02/paypal-express-checkout-with-ruby-on-rails-and-paypal-sdk-merchant/) or [Stripe](https://docs.stripe.com/checkout/quickstart). +Step 4 would be done via integration of something like [the Paypal API](http://coding.smashingmagazine.com/2011/09/05/getting-started-with-the-paypal-api/), via [the paypal-express gem](https://github.com/nov/paypal-express), [the paypal-sdk-merchant SDK](https://www.tommyblue.it/2013/07/02/paypal-express-checkout-with-ruby-on-rails-and-paypal-sdk-merchant/), or [Stripe](https://stripe.com/docs/checkout/guides/rails). ### Assignment @@ -34,7 +34,7 @@ For the first screen, you will be creating 4 dropdown menus that will allow user 1. Seed your database with flights. 1. You will search and view results in the same page. Set up your FlightsController and routes to make the Index page (`/flights`) the root route. 1. Create your search form on the `/flights` index page to submit using a GET (not POST) request back to the same URL. -1. Add the four dropdown menus -- a list of departure airports, arrival airports, number of passengers (1-4), and a date dropdown for the flight date. The date dropdown should only include dates that have existing flights. Don't worry about restricting the contents of the airport dropdowns -- you'd normally use JavaScript -- so just allow the user to select any airport. See this [SO post on formatting date in dropdown lists](http://stackoverflow.com/questions/15720940/rails-format-date-in-drop-down-list-in-view), this [quickie SO post on selecting months/years](http://stackoverflow.com/questions/13001904/rails-drop-down-select-month-year) and the Rails [DateHelper API Docs](http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html) for some help with creating Date selects. +1. Add the four dropdown menus -- a list of departure airports, arrival airports, number of passengers (1-4), and a date dropdown for the flight date. The date dropdown should only include dates that have existing flights. Don't worry about restricting the contents of the airport dropdowns -- you'd normally use JavaScript -- so just allow the user to select any airport. See this [Stack Overflow post on formatting dates in dropdown lists](http://stackoverflow.com/questions/15720940/rails-format-date-in-drop-down-list-in-view), this short [Stack Overflow post on selecting months/years](http://stackoverflow.com/questions/13001904/rails-drop-down-select-month-year) and the Rails [DateHelper API Docs](http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html) for some help with creating Date selects. #### Screen 2: pick a flight @@ -56,9 +56,9 @@ Once the user has submitted their chosen flight, it's time to take their booking 1. Set up your `#new` action, which should have received the flight ID and passenger number parameters, and use it to help render a form for a new booking which displays the currently chosen date, airports, flight ID and a set of fields to enter personal information for each passenger. You'll want to create a new blank Passenger object in your controller for each passenger, and then use `#fields_for` in the view to set up the sub-forms. 1. Try submitting the form and check out the parameters in your server. 1. You'll need your `#create` action to create a new Booking (linking it to the appropriate Flight) but it will also need to accept the nested attributes for each of the Passenger objects and create a new Passenger from them. Be mindful of allowing the nested parameters as well. -1. Go to the Booking model and implement the `#accepts_nested_attributes_for` method. See the [Rails API](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html) for examples and this [SO post on nested attributes and mass assignment](http://stackoverflow.com/questions/18540679/rails-4-accepts-nested-attributes-for-and-mass-assignment) for another. +1. Go to the Booking model and implement the `#accepts_nested_attributes_for` method. See the [Rails API](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html) for examples and this [Stack Overflow post on nested attributes and mass assignment](http://stackoverflow.com/questions/18540679/rails-4-accepts-nested-attributes-for-and-mass-assignment) for another. 1. Once your form is successfully submitted, render the booking's `#show` page which displays the booking information (flight and passenger information). -1. Make sure your ticketing flow is working properly. Good work! +1. Make sure your ticketing flow is working properly. Good work! From 8c78fe75bbc890d68309cd7a67a190bcebf41372 Mon Sep 17 00:00:00 2001 From: Della Maret Date: Wed, 10 Jul 2024 10:03:17 -0400 Subject: [PATCH 2/7] fixed lint errors --- .../forms_advanced.md | 66 ++++++++++--------- .../project_flight_booker.md | 1 + 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/ruby_on_rails/advanced_forms_and_activerecord/forms_advanced.md b/ruby_on_rails/advanced_forms_and_activerecord/forms_advanced.md index 510c5c01d2a..f3469cb6321 100644 --- a/ruby_on_rails/advanced_forms_and_activerecord/forms_advanced.md +++ b/ruby_on_rails/advanced_forms_and_activerecord/forms_advanced.md @@ -14,13 +14,13 @@ This section contains a general overview of topics that you will learn in this l - How to whitelist nested parameters. - How to delete records via form fields. -### Prepopulating `select` tags with collections +### Prepopulating select tags with collections Rails provides you with a few handy ways of making dropdown menus which already contain data when the form is loaded (otherwise they're not that useful). Let's say you want to build a New Post form for your blog but you want to be able to select who the author is from among your list of users. You will need to make a dropdown which submits the user's ID as a part of your `params` hash. So you might populate `@users` in your posts controller: -~~~ruby +```ruby # app/controllers/posts_controller.rb ... def new @@ -28,11 +28,11 @@ Let's say you want to build a New Post form for your blog but you want to be abl @post = Post.new end ... -~~~ +``` The bare HTML way is to build a bunch of `