mirror of
https://github.com/iritmaximus/brewery.git
synced 2026-10-12 19:13:49 +03:00
added errors to creating new rating
This commit is contained in:
@@ -9,10 +9,15 @@ class RatingsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
rating = Rating.create params.require(:rating).permit(:score, :beer_id)
|
@rating = Rating.create params.require(:rating).permit(:score, :beer_id)
|
||||||
session[:last_rating] = "#{rating.beer.name} #{rating.score} points"
|
@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
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
|||||||
@@ -1,6 +1,18 @@
|
|||||||
<h2>Create new rating</h2>
|
<h2>Create new rating</h2>
|
||||||
|
|
||||||
<%= form_for(@rating) do |f| %>
|
<%= form_for(@rating) do |f| %>
|
||||||
|
<% if @rating.errors.any? %>
|
||||||
|
<div style="color: red">
|
||||||
|
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<% user.errors.each do |error| %>
|
||||||
|
<li><%= error.full_message %></li>
|
||||||
|
<% end %>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<%= f.select :beer_id, options_from_collection_for_select(@beers, :id, :to_s) %>
|
<%= f.select :beer_id, options_from_collection_for_select(@beers, :id, :to_s) %>
|
||||||
score: <%= f.number_field :score %>
|
score: <%= f.number_field :score %>
|
||||||
<%= f.submit %>
|
<%= f.submit %>
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ Rails.application.routes.draw do
|
|||||||
# root "articles#index"
|
# root "articles#index"
|
||||||
get '/', to: 'breweries#index'
|
get '/', to: 'breweries#index'
|
||||||
get 'signup', to: 'users#new'
|
get 'signup', to: 'users#new'
|
||||||
|
get 'signin', to: 'sessions#new'
|
||||||
delete 'signout', to: 'sessions#destroy'
|
delete 'signout', to: 'sessions#destroy'
|
||||||
resources :ratings, only: [:index, :new, :create, :destroy]
|
resources :ratings, only: [:index, :new, :create, :destroy]
|
||||||
resource :session, only: [:new, :create, :destroy]
|
resource :session, only: [:new, :create, :destroy]
|
||||||
|
|||||||
Reference in New Issue
Block a user