DEV Community

austinmei5
austinmei5

Posted on

Helper Methods: Part 2

form_with(url: movies_path)

  • 3 variations of finding an active record item: @the_movie = Movie.where(id: params.fetch(:id)).first

@the_movie = Movie.find_by(id: params.fetch(:id))

@the_movie = Movie.find(params.fetch(:id))

  • nested hashes

Red

Blue

Submit
</form

  • top level key is zebra, information/attributes is stored in sub-hashes

  • ex: { :movie => { :title => "Some title", :description => "Some desc" }}

  • Using this format, we can complete mass assignment in the controller. ex:

movie_attributes = params.fetch(:movie)

@movie = Movie.new(movie_attributes)

  • in order to do this however, we need to whitelist the :title and :description columns as strong parameters:

movie_attributes = params.require(:movie).permit(:title, :description)

@movie = Movie.new(movie_attributes)

  • finally, in the routes page, we can represent all standard routes using a single line of code:

resources :movies

Top comments (0)