Note that there are some explanatory texts on larger screens.

plurals
  1. PORails - Getting an error message from the model that is not a Validation error
    primarykey
    data
    text
    <p>So I have a method in a reservation model called add_equip. This method does some checking to make sure the added piece of equipment is valid (not conflicting with another reservation). </p> <p>The checks work. If a added piece of equipment shouldn't be added it isn't, and if it should it is. </p> <p>The problem is I can't figure out how to send the messages back up to the controller to be put in the flash message? I know I must be missing something here, but I've googled for a few hours now and can't really find any clear explanations how how to pass errors back up the the controller, unless they are validation errors.</p> <p><strong>add_equip in reservations_controller</strong></p> <pre><code> def add_equip @reservation = Reservation.find(params[:id]) @addedEquip = Equip.find(params[:equip_id]) respond_to do |format| if @reservation.add_equip(@addedEquip) flash[:notice] = "Equipment was added" format.html { redirect_to(edit_reservation_path(@reservation)) } else flash[:notice] = @reservation.errors format.html { redirect_to(edit_reservation_path(@reservation)) } end end end </code></pre> <p><strong>add_equip in reservation model</strong></p> <pre><code>def add_equip equip if self.reserved.find_by_equip_id(equip.id) self.errors.add_to_base("Equipment Already Added") return false elsif !equip.is_available?(self.start, self.end) self.errors.add_to_base("Equipment Already Reserved") return false else r = Reserved.new r.reservation = self r.equip = equip r.save end end </code></pre> <p>Any help would be greatly appreciated. I know I'm missing something basic here.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload