30
loading...
This website collects cookies to deliver better user experience
rails new project_name
class User < ApplicationRecord
has_many :movies
has_many :reviews
has_many :reviewed_movies, through: :reviews, source: :movie
has_many :genres, through: :movies
end
class Movie < ApplicationRecord
belongs_to :user
belongs_to :genre
has_many :reviews
has_many :users, through: :reviews
end
class Review < ApplicationRecord
belongs_to :user
belongs_to :movie
end
class Genre < ApplicationRecord
has_many :movies
has_many :users, through: :movies
end
_form.html.erb
<%= 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 %>
<%= render partial: "form", locals: {movies: @movies}%>