forked from lighthouse-labs/jungle-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
guub
committed
Nov 14, 2016
1 parent
1d5c62c
commit 63b28cc
Showing
11 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ def index | |
|
||
def show | ||
@product = Product.find params[:id] | ||
@review = Review.new | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class ReviewsController < ApplicationController | ||
before_action :require_login | ||
|
||
def create | ||
@product = Product.find(params[:product_id].to_i) | ||
@review = @product.reviews.new(review_params) | ||
@review.user_id = current_user.id | ||
if @review.save | ||
redirect_to product_path(@product), notice: "Review submitted" | ||
else | ||
render @product | ||
end | ||
end | ||
|
||
def destroy | ||
@review = Review.find params[:id] | ||
@review.destroy | ||
redirect_to product_path(params[:product_id]) | ||
end | ||
|
||
private | ||
|
||
def review_params | ||
params.require(:review).permit(:description, :rating) | ||
end | ||
|
||
def require_login | ||
unless current_user | ||
flash[:error] = "You must be logged in to access this section" | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Review < ActiveRecord::Base | ||
|
||
belongs_to :product | ||
belongs_to :user | ||
|
||
validates :product_id, presence: true | ||
validates :user_id, presence: true | ||
validates :rating, presence: true | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<% if @product.reviews.length === 0%> | ||
<p>There are no reviews!</p> | ||
<% end %> | ||
|
||
<% @product.reviews.reverse_each do |review| %> | ||
|
||
<h4> | ||
<%= review.user.first_name %> | ||
</h4> | ||
<%= review.description %> | ||
</dd> | ||
<dt>Rating | ||
</dt> | ||
<dd> | ||
<%= review.rating %> | ||
</dd> | ||
<% if current_user && review.user_id.to_i == current_user.id%> | ||
<td> | ||
<%= link_to fa_icon('trash'), [@product, Review.find(review.id)], class: 'btn btn-sm btn-danger', method: :delete, data: { confirm: "Are you sure?" } %> | ||
</td> | ||
<% end %> | ||
<br> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateReviews < ActiveRecord::Migration | ||
def change | ||
create_table :reviews do |t| | ||
t.references :product, index: true, foreign_key: true | ||
t.references :user, index: true, foreign_key: true | ||
t.integer :rating | ||
t.text :description | ||
|
||
t.timestamps null: false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters