0

I'm working on my app, where User could accept or reject offer from other users, but something went wrong because, when I create for example 5 offers to job and I try to reject or accept for example third offer, that changes status of the first offer and I don't know what is going on? I follow this topic Product orders between 2 users, so maybe I understood something wrong? please help me

my models:

#user
has_many :jobs_given, class_name: 'Job'
has_many :offers_recived, class_name: 'Offer', through: :jobs_given, source: :offers

has_many :offers_made, class_name: 'Offer'
has_many :jobs_take, class_name: 'Job', through: :offers_made, source: :job

#job
belongs_to :seller, class_name: 'User', foreign_key: :user_id
has_many :offers
has_many :buyers, class_name: 'User', through: :offers

#offer
belongs_to :job
belongs_to :buyer, class_name: 'User', foreign_key: :user_id
has_one :seller, class_name: 'User', through: :job

def accept
    self.status = true
    save
end

def reject
    self.status = false
    save
end

my controller:

def create
    @job = Job.find(params[:job_id])
    @offer = @job.offers.create(offer_params)
    @offer.user_id = current_user.id

    if @offer.save
        redirect_to @job
    else
        redirect_to :back
    end
end

def accept
    offer = current_user.offers_recived.find(params[:id])
    offer.accept
    flash[:success] = "Offer accepted!"
    redirect_to :back
end

def reject
    offer = current_user.offers_recived.find(params[:id])
    flash[:notice] = "Offer rejected!"
    offer.reject
    redirect_to :back
end

private

def offer_params
    params.require(:offer).permit(:pirce_offer, :status, :job_id, :user_id)
end
end

my view:

 #offers/form
= simple_form_for([@job, @job.offers.build]) do |f|
= f.input :pirce_offer
= f.button :submit

#offers/offer
%p= offer.pirce_offer

= link_to "Accept this offer", accept_job_offer_path(offer, @job), method: :post
= link_to "Reject this offer", reject_job_offer_path(offer, @job), method: :post

#jobs/show
= render @job.offers

= render 'offers/form'

@EDIT

 Parameters: {"authenticity_token"=>"YB0i2/JmroyZBAOOyCom5NDwjxrPsGRFV6Uucnp1dNKmIGgED3H4cB7f2fnutRKCh2OjyQICQghM4Zoy13zZrw==", "job_id"=>"5", "id"=>"1"}
User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Offer Load (0.1ms)  SELECT  "offers".* FROM "offers" INNER JOIN "jobs" ON "offers"."job_id" = "jobs"."id" WHERE "jobs"."user_id" = ? AND "offers"."id" = ? LIMIT 1  [["user_id", 1], ["id", 1]]
(0.1ms)  begin transaction
SQL (0.1ms)  UPDATE "offers" SET "status" = ?, "updated_at" = ? WHERE "offers"."id" = ?  [["status", "f"], ["updated_at", "2015-10-06 08:12:40.425741"], ["id", 1]]
2
  • Well, seems that you have some debugging to do. What params arrive to your accept method? What generates them? And so on. Commented Oct 6, 2015 at 8:06
  • I added it to my question Commented Oct 6, 2015 at 8:21

2 Answers 2

2

changes status of the first offer

Always remember that in computing, it's your fault if something doesn't work correctly.

Systems are extremely versatile and flexible to the degree that unless you have systemic issues, there's no reason why it wouldn't work.


ID

The issue is almost certainly to do with the id you're passing:

"job_id"=>"5", "id"=>"1"

I don't know what your data looks like, but this is created by this link_to:

= link_to "Accept this offer", accept_job_offer_path(offer, @job), method: :post

Your accept_job_offer_path would - IMO - have the params as [:job_id] and [:(offer)id] respectively. You're sending them the other way around.

Try this:

= link_to "Accept this offer", accept_job_offer_path(@job, offer), method: :post
= link_to "Reject this offer", reject_job_offer_path(@job, offer), method: :post

This is assuming that your data structure is correct.


Data

There are some other issues I can see:

current_user.offers_recived.find(params[:id])

You're calling current_user.offers_received without indicating the job which received the offer. Surely you'd want to include the job in the process?

Further, you could DRY up your action quite a bit:

#config/routes.rb
resources :jobs do
   match :offer, action: :update, via: [:post, :delete] #-> url.com/jobs/:job_id/offer
end

#app/controllers/jobs_controller.rb
class JobsController < ApplicationController
   def offer
      @job = Job.find params[:job_id]
      offer = current_user.offers_recived.find(params[:id])
offer.accept
      if request.post?
         offer.accept
      elseif request.delete?
         offer.reject
      end
   end
end
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you've nested resources with :offers nested under :jobs you've got the path the wrong way wrong in your "Accept this offer" and "Reject this offer" links.

This means that you're sending the id of the offer as params[:job_id] (except it won't have an id yet because you only just built it) and then params[:id] is the id of @job. This is why it is updating the wrong offer - it's because it is updating the offer with the id matching @job.

How you're going to find an offer by id when you've only just built it in the form is probably the next step along the road.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.