Reputation: 1071
My OrdersController is as follows below, but I keep getting this message:
undefined method `listing_id=' for #
Extracted source (around line #31): 29 30 31 @order.listing_id = @listing.id
Is there something I am doing incorrectly? Am following a tutorial so followed the instructions, then when it wasn't working decided to copy and paste, and it's still not working. Please, any help is appreciated.
Full code is as follows
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def sales
@orders = Order.all.where(seller: current_user).order("created_at DESC")
end
def purchases
@orders = Order.all.where(buyer: current_user).order("created_at DESC")
end
# GET /orders/new
def new
@order = Order.new
@listing = Listing.find(params[:listing_id])
end
# POST /orders
# POST /orders.json
def create
@order = Order.new(order_params)
@listing = Listing.find(params[:listing_id])
@seller = @listing.user
@order.listing_id = @listing.id
@order.buyer_id = current_user.id
@order.seller_id = @seller.id
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]
begin
charge = Stripe::Charge.create(
:amount => (@listing.price * 100).floor,
:currency => "usd",
:card => token
)
flash[:notice] = "Thanks for ordering!"
rescue Stripe::CardError => e
flash[:danger] = e.message
end
respond_to do |format|
if @order.save
format.html { redirect_to root_url, notice: 'Order was successfully created.' }
format.json { render action: 'show', status: :created, location: @order }
else
format.html { render action: 'new' }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
@order = Order.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:address, :city, :state)
end
end
Upvotes: 0
Views: 98
Reputation: 365
Check Fields of the Order Model. Do you have listing_id as a column on the orders table ? Check the migration files to make sure that somewhere along the way you have added a "listing_id" field to the "orders" table.
Upvotes: 1
Reputation: 1089
Define strong params:
def order_params
params.require(:order).permit(:address, :city, :state, :listing_id, :buyer_id, :seller_id )
end
Upvotes: 0