Skip to content

Commit

Permalink
added generic rating view
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles-Butler committed Oct 15, 2020
1 parent 2c6df38 commit 67274e6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
7 changes: 5 additions & 2 deletions app/controllers/ratings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
class RatingsController < ApplicationController

def new
@game = Game.find_by_id(params[:game_id])
@rating = @game.ratings.build
if @game = Game.find_by_id(params[:game_id])
@rating = @game.ratings.build
else
@rating = Rating.new
end
end

def create
Expand Down
19 changes: 8 additions & 11 deletions app/models/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ class Game < ApplicationRecord
accepts_nested_attributes_for :platform

validates :title, presence: true
validate :not_duplicate
validates :title, uniqueness: {scope: :platform_id, message: "You have added this game for this system already"}

def new
@game = Game.new
end


def not_duplicate
if Game.find_by(title: title, platform_id: platform_id)
errors.add(:title, "has already been added for that system")
end
end
#game needs to be unique when added



end
#def not_duplicate
# if Game.find_by(title: title, platform_id: platform_id)
# errors.add(:title, "has already been added for that system")
# end
#end
#game needs to be unique when added -> custom scope?

3 changes: 3 additions & 0 deletions app/models/rating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ class Rating < ApplicationRecord

validates :difficulty, presence: true
validates :stars, numericality: {only_integer: true, greater_than_or_equal_to: 0, less_than: 6}

validates :game_id, :difficulty, uniqueness: {scope: :user, message: "You have rated the game at this difficulty already"}

end
17 changes: 15 additions & 2 deletions app/views/ratings/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@

<% if params[:game_id]%>
<h1>Write your Rating for <%=@rating.game.title %></h1>
on <%= @rating.game.platform.console %>
<br></br>

<% else %>
<h1> Create a Rating </h1>
<% end %>
<%= render '/layouts/errors', object: @rating, as: 'object' %>

<%= form_for @rating do |f|%>
<% if params[:game_id] %>
<%= f.hidden_field :game_id%>
<%else%>
<div>
<%=f.label :game_id, "Choose a game" %>
<%=f.collection_select :game_id, Game.all, :id, :title_in_database, include_blank: true %>
</div>
<%end%>

<div>
<%= f.label :difficulty%>
<%= f.text_field :difficulty, size: "4x2"%>
Expand All @@ -24,7 +37,7 @@ on <%= @rating.game.platform.console %>
<%= f.text_area :review, size: "20x4"%>
</div>

<%= f.hidden_field :game_id%>


<br>
<%= f.submit %>
Expand Down

0 comments on commit 67274e6

Please sign in to comment.