mardi 16 mai 2017

Rails how to associate 2 models on create?

I would like to associate Order object wit Dispute Object on create of Dispute but when i go create the object in the log shows:

ActiveRecord::RecordNotFound (Couldn't find Order without an ID)

should i not try to find the order in the method?

Someone know how to associate the objects in the creation?

the Dispute Controller is:

class DisputesController < ApplicationController

  def new

    if current_user.address.blank?
      redirect_to edit_user_path

      flash[:error] = 'fill the address'
    else
      @dispute = Dispute.new

    end
  end 



def create
 @order = Order.find(params[:id])
  if   current_user == @order.buyer
    dispute = @order.dispute.nil? ? Dispute.new : @order.dispute
    dispute.attributes = params[:dispute]
    dispute.user = @order.buyer
    dispute.buyer_name = @order.buyer_name
    dispute.seller_name = @order.seller_name

    if dispute.save
      flash[:success] = 'Dispute Created'
    end

end

The order model

class Order < ActiveRecord::Base

has_one :dispute

end

the dispute model

class Dispute < ActiveRecord::Base

belongs_to :order

end

Aucun commentaire:

Enregistrer un commentaire