From b0ab392fc23b216cb3b17ab1539f106b3e6df734 Mon Sep 17 00:00:00 2001 From: Aaro Koivukangas Date: Thu, 10 Aug 2023 17:01:25 +0300 Subject: [PATCH] added errors to creating new rating --- app/controllers/ratings_controller.rb | 11 ++++++++--- app/views/ratings/new.html.erb | 12 ++++++++++++ config/routes.rb | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/controllers/ratings_controller.rb b/app/controllers/ratings_controller.rb index 2c2931c..c968a95 100644 --- a/app/controllers/ratings_controller.rb +++ b/app/controllers/ratings_controller.rb @@ -9,10 +9,15 @@ class RatingsController < ApplicationController end def create - rating = Rating.create params.require(:rating).permit(:score, :beer_id) - session[:last_rating] = "#{rating.beer.name} #{rating.score} points" + @rating = Rating.create params.require(:rating).permit(:score, :beer_id) + @rating.user = current_user - redirect_to ratings_path + if @rating.save + redirect_to user_path current_user + else + @beers = Beer.all + render :new, status: :unprocessable_entity + end end def destroy diff --git a/app/views/ratings/new.html.erb b/app/views/ratings/new.html.erb index ae52d6b..616ab6f 100644 --- a/app/views/ratings/new.html.erb +++ b/app/views/ratings/new.html.erb @@ -1,6 +1,18 @@

Create new rating

<%= form_for(@rating) do |f| %> + <% if @rating.errors.any? %> +
+

<%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:

+ + +
+ <% end %> + <%= f.select :beer_id, options_from_collection_for_select(@beers, :id, :to_s) %> score: <%= f.number_field :score %> <%= f.submit %> diff --git a/config/routes.rb b/config/routes.rb index 328d85a..6587544 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ Rails.application.routes.draw do # root "articles#index" get '/', to: 'breweries#index' get 'signup', to: 'users#new' + get 'signin', to: 'sessions#new' delete 'signout', to: 'sessions#destroy' resources :ratings, only: [:index, :new, :create, :destroy] resource :session, only: [:new, :create, :destroy]