[Help] Need help getting form to not save to database immediately, or destroy if form elements are not filled

That would be ideal. Here is the controller:

class EngagementsController < ApplicationController before_action :with_includes, only: [:show] load_and_authorize_resource

def index @engagements = @engagements.order(:name) respond_to do |format| format.html format.json { result = @engagements.to_json( only: [:id, :name, :status, :detail], include: [ unread_messages: { only: :id }, messages: { include: [:author, :content] }, user: { only: [:first_name, :last_name, :email] }, experts: { only: [:id, :name, :display_name], methods: [:image_url, :bio, :bio_long, :title] } ] ) render json: result, status: :ok } end end

def create respond_to do |format| if engagement = Engagement.create!(engagement_params) format.html { redirect_to edit_request_path(engagement) } format.json { render json: engagement, status: :created } else format.html { render action: "new" } format.json { render json: engagement.errors, status: :unprocessable_entity } end end end

def update respond_to do |format| send_email = @engagement.status === 'created' && engagement_params[:status] === 'submitted' if @engagement.update(engagement_params) if (send_email) AdminMailer.with(engagement: @engagement).submitted_engagement.deliver_later end format.html { redirect_to edit_request_path, notice: "Expert request was updated" } format.json { result = @engagement.to_json( only: [:id, :name, :status, :detail], include: [ unread_messages: { only: :id }, messages: { include: [:author, :content] }, expert_requests: { only: [ :id, :engagement_id, :expert_id, :status], include: [ expert: { only: [ :first_name, :last_name, :display_name, :attr_centre ], include: [ hcp_type: { only: [:name] }, country: { only: [:name] }, specialties: { only: [:name] } ], methods: [:image_url, :bio] } ] } ] ) render json: result, status: :ok } else format.html { render action: "edit" } format.json { render json: @engagement.errors, status: :unprocessable_entity} end end end

def destroy respond_to do |format| if @engagement.destroy format.html { redirect_to requests_path, notice: "Request was cancelled" } format.json { render json: { message: 'request deleted'}, status: :ok} else format.html { render action: "edit" } format.json { render json: @engagement.errors, status: :unprocessable_entity } end end end

def cancel respond_to do |format| @engagement.status = 'cancelled' if @engagement.save format.html { redirect_to request_path, notice: "Expert request was cancelled" } format.json { render json: { message: 'request cancelled'}, status: :ok} else format.html { render action: "edit" } format.json { render json: @engagement.errors, status: :unprocessable_entity } end end end

def open respond_to do |format| @engagement.status = 'active' if @engagement.save format.html { redirect_to request_path, notice: "Expert request was opened" } else format.html { render action: "edit" } end end end

def submit respond_to do |format| @engagement.status = 'submitted' if @engagement.save format.html { redirect_to request_path, notice: "Expert request was submitted" } else format.html { render action: "edit" } end end end

def status respond_to do |format| if @engagement.update(engagement_params) format.html { redirect_to requests_path, notice: "Expert request status was updated" } format.json { render json: @engagement, status: :ok} else format.html { render action: "requests_path", notice: "Error updating expert request status" } format.json { render json: @engagement.errors, status: :unprocessable_entity} end end end

private

def with_includes @engagement = Engagement.includes(messages:[:author]).find(params[:id]) end

def engagement_params params.require(:engagement).permit( :name, :user_id, :status, detail: {}, expert_ids:[] ) end

end

/r/rails Thread Parent