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.
@job.offers, eachfin that loop is anoffer. (Please use descriptive variable names, eg "offer", it makes things so much more readable). You then doif @job.offers.find(f).status- this is very odd. Why are you looking it up again? Why not just sayif f.status == false? Also, you can useif f.status.blank?instead of testing whether it's equal to false or nil.f.status == false || f.status == nil || f.status.blank?, it doesn't work, I mean theformis not rendering, whenstatus.blank?