1

I have a job model which has_many offers, User could accept or reject these offers, but how can I do that when User accept an offer, the more offers cannot be created? I try to do this in that way, it works but it duplicates input boxes. So what I'm doing wrong or how can I do it in another way?

#show.html.haml
- if @job.offers.blank?
  = render 'offers/form'

- @job.offers.each do |f|
  - if @job.offers.find(f).status == false || @job.offers.find(f).status == nil 
    = render 'offers/form'
  - else
    cannot do that

 #offers/_form.html.haml
 = simple_form_for([@job, @job.offers.build]) do |f|
   = f.input :pirce_offer
   = f.button :submit
2
  • When you iterate over @job.offers, each f in that loop is an offer. (Please use descriptive variable names, eg "offer", it makes things so much more readable). You then do if @job.offers.find(f).status - this is very odd. Why are you looking it up again? Why not just say if f.status == false? Also, you can use if f.status.blank? instead of testing whether it's equal to false or nil. Commented Oct 7, 2015 at 9:20
  • Ohh that's right. But when I test f.status == false || f.status == nil || f.status.blank? , it doesn't work, I mean the form is not rendering, when status.blank? Commented Oct 7, 2015 at 9:29

1 Answer 1

1

You need to equate yourself with scoping in programming; whenever you create a "block" in Ruby, it's essentially like invoking a function, with its own local scope:

- @job.offers.each do |f|
  - if f.status == false || f.status == nil 
    = render 'offers/form'
  - else
    cannot do that

As an aside to this, I don't get the purpose of calling a form that builds a new offer?

You're looping through an offer of a job, using the status conditional to determine whether the user should create an offer or not.... yet, surely if the offer exists, the user has already posted an offer?

Also, you're using a completely fresh set of data for the form:

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

Shouldn't you be using something like the following:

 = simple_form_for([@job, f]) do |f|
   = f.input :pirce_offer
   = f.button :submit

Okay I get it -- you have a sort of "auction" system where offers are accepted/declined. This would give you the ability to make a new offer if the previous was rejected........... it's still sketchy as hell.


how can I do that when User accept an offer, the more offers cannot be created

You'll need to use some validation in your view and model.

Something like this:

#app/models/offer.rb
class Offer < ActiveRecord::Base
   validates :user_id, uniqueness: { scope: :job, message: "Only one offer per job" }
end

I could create a much more complete answer if you describe how you want the jobs/offers to work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I want to do that: User(Seller) can create job, users(Buyers) of this job could create offers and when Seller accepts offer, the ability of making new offers to this job is blocked.

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.