Git Product home page Git Product logo

crud-with-validations-lab's Introduction

CRUD With Validations Lab

Introduction

Your goal in this lab is to create a thorough CRUD interface for one model, the Song.

Songs

Songs have the following attributes and limitations:

  • title, a string
    • Must not be blank
    • Cannot be repeated by the same artist in the same year
  • released, a boolean describing whether the song was ever officially released
    • Must be true or false
  • release_year, an integer
    • Optional if released is false
    • Must not be blank if released is true
    • Must be less than or equal to the current year
  • artist_name, a string
    • Must not be blank
  • genre, a string

Requirements

Use the resource generator, not the scaffold generator

  1. Define a model with validations for Song.

  2. Define all RESTful routes for songs.

  3. Render the list of songs in an HTML table.

  4. Build views that connect to each other using route helpers.

  5. Use form_for to build forms with pre-fill and error list features. (Hint: Try using a partial to cut down on copy/pasting!)

  6. Allow deleting songs with a link, using link_to. Check out the official documentation for additional info including setting the method: to :delete.

  7. Use strong parameters in your POST/PATCH controller actions.

  8. Set the root route to the song index.

crud-with-validations-lab's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

crud-with-validations-lab's Issues

JQuery UJS No Longer Required

It appears as though Rails has incorporated everything that was done by JQuery UJS into Rails UJG, and, based on my own solution to this lab, it looks like students no longer have to add any additional functionality to their app in order to get the link_to delete functionality. By simply adding method: delete I was able to pass the delete specs.

rails/rails#25208

`link_to` does not delete a song in newer versions of Rails, unless jQuery is used.

Hi. One problem that I had with this lab is that I tried to use link_to to delete a song from the index page and the show page. Here's the code that I used:

<%= link_to 'Delete song', song, method: :delete, 
data: { confirm: 'Are you sure you want to delete this song?' } %>

This generated <a> elements such as:

<a data-confirm="Are you sure you want to delete this song?" rel="nofollow" 
data-method="delete" href="/songs/3">Delete song</a>

I had my routes set up correctly, and I had a corresponding destroy action in my SongsController. Despite all of that, every time I clicked on "Delete song", it redirected to the song's show page instead of going to the destroy action, and it did not show that "Are you sure you want to delete this song?" message.

After I screen-shared with three Technical Coaches, we finally figured out that newer versions of Rails (5 and above, I think) use jQuery to make all of this work. Without jQuery, the <a> element generated by link_to goes to the show action by default, instead of destroy.

In order to solve this problem, we had to do three things:

  1. Add gem 'jquery-rails' to the Gemfile and run bundle.
  2. Create an /app/assets/javascripts/application.js file and add this code to it:
//= require jquery
//= require jquery_ujs
//= require_tree .

(Note: It's possible that only the second line of code is what's needed, from what I've read online.)

  1. Add this line of code to the /app/views/layouts/application.html.erb file:
  <%= javascript_include_tag 'application.js' %>

(In my case, we put it inside of the <head> tag, below the stylesheet_link_tag.)

After we did all that, the lab worked as it should. Here's the Stack Overflow article that helped us: https://stackoverflow.com/questions/12997167/ruby-on-rails-link-to-delete-method-not-working

Long story short, the README and/or lab itself needs to be updated. Thanks for looking into this!

---Sdcrouse

Lab passed without fulfilling all of the requirements

There doesn't seem to be enough tests to ensure that all of the requirements are fulfilled. I was able to get the test to pass without using strong params or error lists. Also, the delete portion that requires javascript seems ambiguous.

Link_to create new song

All tests are passing without link_to create new song in view files even though lab requirements dictate connect view files to other view files.

This lesson is out of sequence... it refers to partials which aren't discussed until later in curriculum

In addition to talking about partials, it also references button_to to build forms which also hasn't been discussed yet at this point in the curriculum. This also applies to the reference to "include your new assets in the application.html.erb file." At this point in the curriculum there has been no discussion of Javascript.

It can be jarring (and frustrating) to have some concepts very clearly explained in READMEs and then have something be required for a lab which hasn't even been talked about yet and have to rely on what you can find through Google or Stack Overflow. Sometimes those are helpful, sometimes not so much. It really brings learning to a grinding halt.

It looks like the references to Javascript were removed from the ReadMe but the Javascript still remains in the solution file. They should match. If the lab doesn't require JS because it hasn't been discussed in the curriculum, it shouldn't be part of the solution file. (This has happened a couple of times in the Rails lab solutions.)

I like to compare my completed lab to the solution when everything passes to see how my version compares to the solution and it is useless if the solution uses something more efficient but hasn't been covered in the curriculum.

Need to include the jquery-rails gem in the Gemfile for this lab

This lab explicitly states to use the link_to method in order to delete a Song from the database. But in order to do this, JavaScript is required as link_to uses JavaScript functionality in Rails. This lab's Gemfile does not have any JavaScript gem included and thus in order to get the link_to to work, it has to be added. You need to do the following to get the link_to functionality to work properly:

  1. Add gem 'jquery-rails' to the Gemfile

  2. Create an application.js file inside the apps\assets\javascripts folder and input the following lines:

//= require jquery
//= require jquery_ujs

  1. Add the following line to the views\layouts\application.html.erb file

<%= javascript_include_tag "application" %>

link_to 'Delete' doesn't work

Regarding this line of code in crud-with-validations-lab-v-000/app/views/songs/index.html.erb (line 27):

<%= link_to 'Delete', song, method: :delete, data: { confirm: 'For real?' } %>

I was having some trouble with this requirement "Allow deleting songs with a link, using link_to", so I pulled down the learn-co solution and found that it doesn't work either. The delete link created here goes to the show page rather than the destroy action.

I found this on SO:
http://stackoverflow.com/questions/18154916/rails-4-link-to-destroy-not-working-in-getting-started-tutorial
but even doing what's suggested in this post, I couldn't get it working.

"tbody > tr"

Please put that the index page requires a table output to help people understand the errors!

Known Issues

Problem: Some tests missing
Solution: Need tests for show songs page

Problem: Versioning may be incorrect for some solutions
Solution: Ensure current set up is functioning, or update gemfile (rails version)

Problem: Some persistent issues referring to jQuery.
Solution: Check if some batches are not receiving auto-redeploy as jquery should have been removed months ago, but may not have trickled down to all courses (cpb maybe?)

Reason for including assets is unclear

Step 6 of the instructions states:

Be sure to include your new assets in application.html.erb

As far as I can tell, none of the tests require JavaScript, so the reason for doing this is unclear. Is it just to get students accustomed to using Rails helpers, or is it actually required for some reason?

Reported by @Sdcrouse.

Delete not functional

Hello this lab passes without having a functional delete.

Here's how I solved it:

Gemfile:
gem 'jquery-rails'

app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs

Note: Sometimes rails generators have model.js you can rename to application.js

app/views/layouts/application.html.erb
<%= javascript_include_tag "application" %>

application.js not initially included

I might be missing something here, but I thought that application.js was supposed to be included as a default template file for every new rails application. It did not seem to be included here - this confused me when I was asked to put some text into application.js and it was nowhere to be found?

Lackluster Testing

I was able to get all tests passing without fully implementing all the validations specified in the instructions. Specifically, I didn't add a validator for whether or not a song was repeated by the same artist in the same year, and I didn't add a validator to check whether release_year is not blank if released is true. Looking through the file structure, there don't seem to be any specs for the model whatsoever.

Master branch of lab is missing jquery needed to implement delete route

The master and solution branches for this lab differ regarding jquery inclusion. The solution branch has a commit from March 2016, "Include jquery_ujs so deletion works." Files associated with this change are necessary to implement the delete action written as one of the lab's requirements. Since a student forks the master branch when opening the lab, attempts to actually 'see' the delete action work in-browser/in console will fail (because the jquery gem and javascript loading via layout.html.erb aren't implemented in the master branch). Lab tests will pass--it's just browser action that's affected. Can the layout.html.erb, application.js, and Gemfile (all master branch) be modified as needed to match the solution branch?

too much too fast here?

The Delete lab speaks about a method that works with javascript and rails

From delete lesson
The data-confirm attribute and the data-method attribute rely on some JavaScript built into RailsI

Example they share
<% @people.each do |person| %>

<%= person.name %> <%= link_to "Delete", person, method: :delete, data: { confirm: "Really?" } %>

<% end %>

In the writeup for this lesson - there is a bullet 5
Allow deleting songs with a link, using link_to. (HINT: You might need to add some functionality so that you can properly delete a song using just a link. Be sure to include your new assets in application.html.erb.)

I was able to make my delete logic work but not at all using the data mentioned above from the delete lesson. The writeup for this lab shows some docs that feel super confusing and I think this more elegant delete method (in the form_for helper) probably shouldn't get discussed/expected until we get to the rails and javascript portion.....it seems too advanced to figure out just based on the confusing docs shared.

js issue

Hey, in this lab, the js-fix is on the solution files, but it hasn't been incorporated into the master file, and pipeline assets and js haven't yet been covered in the curriculum. It became an awesome experience, because one of the TC's, Matt, when through the whole pipeline creation process, but probably, the fixes should be moved into the master files that we students download. Thanks!

Recommend more encompassing tests and more explicit guidance

Hi, I just want to re-iterate and add some details to the previous issue posted. Tests are currently able to pass without meeting requirements - I specifically noticed that I could write nothing in my show.html.erb file at all and the tests would pass. This allows the lab to be passed without properly setting up the link_to with the method: :delete option, which requires more action than is provided in the lab. To make it work, I created an application.js file, and added the lines:

//= require jquery
//= require jquery_ujs

Then, I added
<%= javascript_include_tag 'application' %>
to the application.html.erb file (which is the only part which is touched on in the lab, but it doesn't say what to put in the javascript_include_tag method)

Lastly, I had to add 2 gems to my gemfile to get everything to work properly: 'jquery-rails' and 'rails-ujs'

I think this lab might need a little more guidance about how to get the :method => :delete option in the link_to working, as well as a touch up on tests to ensure that all requirements are met before it can be passed. Thanks!

Test Suite for show view file is not active

I deleted my code for the show view file after completing the lab to reach better understanding and I discovered that there isn't a test suite for the show view file. Is this done on purpose?

With_options method not explained

with_options if: :released? do |song|
song.validates :release_year, presence: true
song.validates :release_year, numericality: {
less_than_or_equal_to: Date.today.year
}

not talked about in the lessons. It was in section 5 of the rails validation docs. You only told us to focus on section 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.