Git Product home page Git Product logo

partial-locals-lab's Introduction

Partials with Locals Lab

Now that we learned about locals, let's refactor our old codebase and add a couple new features using this new tool.

Objectives

  1. Use the locals keyword
  2. Understand why using instance variables in partials is bad
  3. Use a partial to iterate over a collection, passing in a local
  4. Use a partial from another controller with a local

Overview

So your team's lead engineer looked over the codebase and asked you to not refer to instance variables in your partials but rather to pass through local variables. That way, your code will be more explicit about its dependencies when it calls the partial.

Also, the lead engineer asked for a couple new features.

The first is that we display all students on the classroom show page instead of singling out the oldest student with a special note. The engineer thinks this isn't very polite.

Second, they also want to add some search functionality so that a user can search for a student by name. They'll type the name in in a form field and we'll use the power of ActiveRecord to find matching data. It's OK if other students with similar names are returned in the search results.

Instructions

  1. Refactor the _form.html.erb partial to accept the argument to the form_for helper as a local. You'll also need to change the new.html.erb and edit.html.erb views as well.

  2. Refactor the _student.html.erb partial to pass through each rendered student as a local.

  3. On the classroom show page, iterate through each classroom's students and display each of them using our _student.html.erb partial with locals.

  4. Create a _classroom.html.erb partial to display classroom information on the classroom show page.

  5. Add in search functionality such that users can type in a student name or fragment of a student name and and see all matching results on the students index page. The results should be displayed by rendering a students/_student.html.erb partial.

To start on this last deliverable, add a search form using the form_tag helper to your students index page:

<%= form_tag students_path, method: :get do %>
  <p>
    <%= text_field_tag :query, params[:query] %>
    <%= submit_tag "Search", name: nil %>
  </p>
<% end %>

When this form is submitted, it will make a GET request to /students. The text from the query will be available in the params hash. From here, you'll need to use that query to find students using a "fuzzy" or "wildcard" search in the controller in order to create the set of matches.

For the next step, you'll need to write a flexibly matching (or "wildcard") query in ActiveRecord that follows the form: Student.where("name LIKE ?", "%query%"). For example, Student.where("name LIKE ?", "%M%") will return all students with an "M" anywhere in their name. Remember, your query will come from the params hash.

Once you have the search functionality coded, you should be able to visually test it by visiting http://localhost:3000/students?query=search_text.

Resources

partial-locals-lab's People

Contributors

annjohn avatar blake41 avatar carnold18 avatar dakotalmartinez avatar danielseehausen avatar drakeltheryuujin avatar ihollander avatar jeffkatzy avatar jmburges avatar lizbur10 avatar maxwellbenton avatar perpepajn25 avatar pletcher avatar ritchey0713 avatar

Stargazers

 avatar

Watchers

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

partial-locals-lab's Issues

Classroom Show View doesn't need oldest student

classroom_show_view
renders classroom information on the show view
renders a partial that only contains classroom (not student) information
renders a students/student partial
displays the student information from the partial

but

`
Classroom Info

<%= render partial: "classrooms/classroom", locals: {classroom: @classroom} %>

We now want to call out the oldest student in the class:
<% @classroom.students.each do |student| %>
<%= render partial: 'students/student', locals: {student: student} %>
<% end %>

##14
`

bullet point 5 in instructions insufficient

The method we are required to write requires knowledge of SQL "LIKE operator". Maybe there should be a hint about search queries. Otherwise, bullet point 5 is a bit out of nowhere.

I needed to add routes for my tests to pass

Failures:

  1. Student search when it receives a search query returns all students whose names contain the given string
    Failure/Error: <%= form_tag students_path, method: :get do %>

    ActionView::Template::Error:
    undefined local variable or method `students_path' for #<#Class:0x000055ea769a2b38:0x000055ea75e86b00>
    Did you mean? @Students
    This was also casuing 7 other tests to fail for the exact same reason.

Once adding routes in for students and classrooms

Rails.application.routes.draw do

For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

resources :classrooms
resources :students
end

It passed all tests.

The lab never specifically mentions needing to create routes, and im not sure if it has other interactions with the previous tests given i was forced to use the solution branch to try to figure out why what looked to be correct code kept failing.

Search

It appears to me that the lesson requests a search for classrooms, but the tests are for a search for student names.

possible to pass search spec without implementing functioning search

The student_index_spec.rb file checks for the presence of a search method and rendering of a partial--but there isn't a check to see that the search is actually implemented as described by the README ("Second, they also want to add some search functionality so that a user can search for a student by name. It's okay if other students with similar names are returned in the search results.")

It's possible to pass all specs for this lab without updating student#index in student_controller.rb to use the model's search method. i.e., leaving student#index defined as @students = Student.all. When rendered in-browser, though, the search form doesn't actually work. Not sure what test could be added...maybe one that visits students/index, fills the search form with a name, then checks that the last response does not include a name that wouldn't otherwise be returned?

Update to Faker Gem

There was an update to the Faker gem that requires the keywords 'from' and 'to" in order for rake db:seed to work. Update the code in the db/seeds.rb file to match the below

80.times do Student.create(name: Faker::Name.name, hometown: Faker::Address.city, birthday: Faker::Date.between(from: 25.years.ago, to: 18.years.ago)) end

Unable to pass variable @students from classroom controller

For the classroom#show view using the student partial, I set up my classroom controller like so:
def show
@classroom = Classroom.find(params[:id])
@Students = @classroom.students
end

I then iterated through @Students and used each individual student in the student partial. While this works in my browser as expected, the tests failed and kept giving me an error that @Students was nil. Testing it in pry and the console showed that @classroom.students gave me back a full list of students, and it showed all students in the browser.

The only way I could pass the tests was to use @classroom.students.each to iterate on the show page rather than passing the @Students variable I created in the controller. A technical coach helped me figure out the problem and suggested that it could be something specific in the tests that is not allowing the use of a variable @Students.

test for search feature too loose

you can pass all of the tests without actually building the search feature. if you have partials for _student and render that partial on the student index page, plus .search method in Student class, you can pass tests without building /students/search route, #search method in students controller, search for on students index page.

Lack of specificity

Just to echo all the previous issues, the instructions to "implement search functionality" leave us guessing what the tests are actually looking for. The tests end up not even looking for something that will actually work for an end user.

You could add something simple in to make it work for the end user, for example, the path http://localhost:3000/students/?search=QUERY can be used to set @Students in the index controller by whether or not params[:search] is nil (ie Student.all vs Student.search(params[:search]). That path will already automatically be routed to the students#index action. This easily works visually in the browser and a few sentences explaining it will help students understand what the requirements are rather than digging through the spec files.

One of the reasons it is confusing is because search functionality is never actually implemented beyond the model.

Thanks!

Missing keywords in seeds.rb

Missing keywords :from and :to in the 80.times do block

Currently:

80.times do
Student.create(name: Faker::Name.name, hometown: Faker::Address.city, birthday: Faker::Date.between(25.years.ago, 18.years.ago))
end

Should be:

80.times do
Student.create(name: Faker::Name.name, hometown: Faker::Address.city, birthday: Faker::Date.between(from: 25.years.ago, to: 18.years.ago))
end

Search functionality requirement is unclear

The following lines make it seem as though we need to set up a form or somehow receive user input on the students/show or students/index pages. However, this is not necessary and so the instructions are really confusing regarding what specifically we need to do, or what the desired end result is:

add some search functionality so that a user can search for a student by name. It's okay if other students with similar names are returned in the search results.
Add in search functionality such that users can search for a student by name and see all matching results on the students index page. The results should be displayed by rendering a students/_student.html.erb partial.

Iterating vs. rendering collection

I believe the way the spec is written is a little bottle-necky. The tests only pass if the @students variable is iterated through, versus using collections notation i.e. <%= render partial: "student", collection: @students %> . Don't they do the same thing?

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.