DEV Community

loading...
Cover image for Phase 3 of FlatIron

Phase 3 of FlatIron

Jessica Joseph
Junior Software Engineer. Just starting out in this enormous world of coding.
・4 min read

This is my third project I created since starting my coding journey! It's crazy to think that I started with no knowledge of coding and now I have just completed my third project for FlatIron School. For Phase 3, we were tasked with creating a Ruby on Rails application. I created an app called 'Five Star Movies', which users can create an account to review and rate movies. I was really excited to start building this app, because I felt as if all my knowledge that I learned over the past few months have finally come together. With that being said, I successfully created a ruby on rails application that reviews movies!

Silly me to think that this project was not going to present me with many issues, because that was totally not the case. I was not aloud to create the app with Scaffold, because, well that basically does all the work for you! Building it from the ground up was very challenging but also very rewarding. I ran across a lot of errors and mishaps, but it's all done now and hopefully some of these tips can help others in the future!

Setting Up Your App

Once you have your idea for you application and are ready to start building, where do you start? The first thing you want to do is in your console run:

rails new project_name
Enter fullscreen mode Exit fullscreen mode

What this will do is, populate the foundation of your application and will create the basic files and folders that you will need to build it. If you are familiar with Sinatra, it's almost like how the corneal gem will populate the basics of your Sinatra app. Pretty neat!

Setting Up Your Associations

This part is very important for the functionality of your app. Once you've created all your, model, view and controllers, in the models, you will need to set up your belongs_to, has_many and has_many :through associations. Associations are an important part of building this app, because it will let the program know how each of the separate files are connected to one another. Without setting these up, your application will not be able run because your files don't know that they are all working as a collective. In my app "Five Star Movies", I have a user, movie, review and genre models. My associations are set up as follows:

class User < ApplicationRecord 
    has_many :movies
    has_many :reviews 
    has_many :reviewed_movies, through: :reviews, source: :movie 
    has_many :genres, through: :movies 
end
Enter fullscreen mode Exit fullscreen mode
class Movie < ApplicationRecord
    belongs_to :user
    belongs_to :genre
    has_many :reviews 
    has_many :users, through: :reviews
end
Enter fullscreen mode Exit fullscreen mode
class Review < ApplicationRecord
    belongs_to :user
    belongs_to :movie 
end
Enter fullscreen mode Exit fullscreen mode
class Genre < ApplicationRecord
    has_many :movies 
    has_many :users, through: :movies 
end
Enter fullscreen mode Exit fullscreen mode

Partials

Partials come in handy quite a bit when building a rails application. In your view files, when you have the same bit of code written in multiple spots, that is usually a firm indication that you will need to set up a partial. When creating the erb file for your partial, always make sure to name the file with an underscore before the filename. In my application, a place where I created a partial was in the views/movies. For both my new and edit erb files, I had the same block of code for the form to either create or edit a movie. With that being said I created a new view/movie file called

_form.html.erb
Enter fullscreen mode Exit fullscreen mode

I copied the code that I had for my new movie form and pasted it in the new partial that was created.

<%= form_for @movie, html: { multipart: true } do |f| %>

  <%= f.label "Choose a genre:" %>
  <%= f.collection_select :genre_id, Genre.all, :id, :name, include_blank: true%>

  <p>Or create a new genre:
  <%= f.fields_for :genre do |g| %>
    <%= g.text_field :name %>
  <% end %>
  </p>
  <br>
  <br>
  <%= f.label :title %>
  <%= f.text_field :title %>
  <br>
  <br>
  <%= f.label :description %>
  <%= f.text_area :description %>
  <br>
  <br>
  <%= f.label :movie_length %>
  <%= f.text_field :movie_length %>
  <br>
  <br>
  <%= f.label :director %>
  <%= f.text_field :director %>
  <br>
  <br>
  <%= f.label :rating %>
  <%= f.text_field :rating %>
  <br>
  <br>
  <%= f.label :image %>
  <%= f.file_field :image %>
  <br>
  <br>
  <%= f.submit %>
<% end %>
Enter fullscreen mode Exit fullscreen mode

With that code now being moved to the partial, I removed the form from both the new and edit erb files. I then rendered the partial in both of those file like such:

<%= render partial: "form", locals: {movies: @movies}%>
Enter fullscreen mode Exit fullscreen mode

This not only made my code look a lot nicer, but if someone else were to go and look at my code, they wouldn't have to scroll through a whole view page to look at the form that is mixed in with other code. Partials make life a little bit easier when building your rails app.

Future Project Plans

Overall, I really enjoyed creating this app and learned a lot while doing it. In the near future I hope to really expand the functionality of the app and add a lot more styling to it. I'm planning on continuing to build this app and I'm going to add new features that will make it an overall better user experience.

You can check out my project repo if you would like at: (https://github.com/jessicaajosephh/FiveStarMovies)

Discussion (0)