If a seller sells something, I'm unable to show the user who bought it.
class UsersController < ApplicationController
before_action :authenticate_user!
def show
@user = User.find_by_name(params[:id])
@product = current_user.products.where(availability: true)
@purchased = Sale.where(buyer_email: current_user.email)
@sales = Sale.where(seller_email: current_user.email)
end
private
def post_params
params.require(:user).permit(:name, :email)
end
end
sales.html.erb:
<% @sales.each do |sale| %>
<tr>
<td><%= link_to sale.product.title, pickup_path(sale.guid) %></td>
<td><%= time_ago_in_words(sale.created_at) %> ago</td>
<td><%= sale.seller_email %></td>
<td><%= sale.amount %></td>
If I go ahead and change it to <%= sale.buyer_email %>
, it just shows me the current user and what THEY just bought rather than WHO bought their item. This is what I get after checking the console, the seller_email
is nil and the amount is nil for the last sale. How do I fix this so that the seller can see who go their item?
>> Sale.last
Sale id: 2, buyer_email: "dave64@gmail.com", seller_email: nil,
amount: nil, guid: "71363abb-8921-3d5c-a4d4-200fa2855ae",
product_id: 2, created_at: "2015-05-24 06:07:23", updated_at: "2015-05-24 06:07:23"
This is the transactions controller
class TransactionsController < ApplicationController
def create
product = Product.find_by!(slug: params[:slug])
token = params[:stripeToken]
begin
charge = Stripe::Charge.create(
amount: product.price,
currency: "usd",
card: token,
description: current_user.email)
@sale = product.sales.create!(buyer_email: current_user.email)
redirect_to pickup_url(guid: @sale.guid)
rescue Stripe::CardError => e
@error = e
redirect_to product_path(product), notice: @error
end
end
def pickup
@sale = Sale.find_by!(guid: params[:guid])
@product = @sale.product
end
end
The is the products controller
class ProductsController < ApplicationController
respond_to :js, :html
def index
@products = Product.where(availability: true)
respond_with(@products)
end
def show
end
def new
@product = Product.new
end
def edit
authorize! :manage, @product
end
def create
@product = current_user.products.new(product_params)
@product.save
respond_with(@product)
end
def update
authorize! :manage, @product
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
def destroy
authorize! :manage, @product
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def product_params
params.require(:product).permit(:title, :image)
end
end
Aucun commentaire:
Enregistrer un commentaire