Skip to content

Commit

Permalink
Adding functionality for ratings and reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
sohomdutta2772 committed Nov 16, 2024
1 parent 709734e commit effae74
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/controllers/item_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def handle_create_listing
end

def show_listing
@item = Item.find(params[:id])
@item = Item.find(params[:id])
@ratings = @item.ratings.includes(:user).order(created_at: :desc)
end

def create_order
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/ratings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class RatingsController < ApplicationController
def create_rating
if !session[:user]
redirect_to login_path
return
end

@item = Item.find(params[:item_id]) # Ensure :item_id is passed correctly
@rating = @item.ratings.build(rating_params)
@rating.user_id = session[:user]["id"]

if @rating.save
redirect_to item_path(@item), notice: "Successfully added rating"
else
redirect_to item_path(@item), alert: "Could not create review"
end
end

private

def rating_params
params.require(:rating).permit(:score, :comment)
end
end
1 change: 1 addition & 0 deletions app/models/rating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class Rating < ApplicationRecord
belongs_to :item

validates :comment, presence: true
validates :score, presence: true, inclusion: { in: 1..5}
end
2 changes: 1 addition & 1 deletion app/views/item/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<p><strong>Description:</strong> <%= item.description %></p>
<p><strong>Price:</strong> <%= number_to_currency(item.price) %></p>
<p><strong>Seller:</strong> <%= item.user.username %></p>
<p><strong>Reviews:</strong> TODO</p>
<p><strong>Reviews:</strong> <%= item.ratings.count %></p>
</div>
</li>
<% end %>
Expand Down
34 changes: 33 additions & 1 deletion app/views/item/show_listing.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,36 @@
<p>Please <a href="<%= login_path %>">log in</a> to place an order.</p>
<% end %>

<p><strong>Reviews:</strong> </p>
<div class="reviews-section">
<h2>Reviews</h2>

<% if session[:user] %>
<div class="review-form">
<h3>Add Your Review</h3>
<%= form_with(url: add_review_path(@item), local: true) do |form| %>
<div>
<%= form.label :score, "Rating (1-5 stars)" %>
<%= form.select "rating[score]", options_for_select(1..5) %>
</div>
<div>
<%= form.label :comment, "Your Review" %>
<%= form.text_area "rating[comment]", rows: 4 %>
</div>
<%= form.hidden_field :item_id, value: @item.id %>
<%= form.submit "Submit Review", class: "submit-button" %>
<% end %>
</div>
<% end %>

<div class="reviews-list">
<% @item.ratings.includes(:user).order(created_at: :desc).each do |rating| %>
<div class="review">
<div class="stars">
<% rating.score.times do %><% end %>
</div>
<p class="review-comment"><%= rating.comment %></p>
<p class="review-author">- <%= rating.user.username %></p>
</div>
<% end %>
</div>
</div>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@
get "item/:id" => "item#show_listing", as: :item
post "item/:id/order" => "item#create_order", as: :create_order

post "item/:id/review" => "ratings#create_rating", as: :add_review

get "profile" => "user#show_profile"
end

0 comments on commit effae74

Please sign in to comment.