ruby on rails - How do I interrupt the destroy method before it puts up the confirmation? -


in view file have

%td= link_to 'destroy', fund_subfund_path(@fund, subfund), :method => :delete, :data => { :confirm => 'are sure?' }

my controller has

def destroy   @subfund = subfund.find(params[:id])   fund_id = @subfund.fund_id   nickname = @subfund.nickname   if @loans.empty?     @subfund.destroy     respond_to |format|       format.html { redirect_to fund_path(fund_id), notice: 'subfund ' + nickname + ' destroyed.' }       format.json { head :no_content }     end   else     respond_to |format|       format.html { redirect_to fund_path(fund_id),          notice: 'subfund ' + nickname + " has loans, can't destroyed." }       format.json { head :no_content }     end   end end 

the redirects , messages work fine. problem if @loans not empty , @subfund.destroy action not carried out, "are sure?" confirmation still appears.

how intercept method before confirmation message?

i did try link_to_if alternative, still displays "destroy" inactive link, think confusing user.

baylor rae's answer, in particular checks in realtime if there loans.

you might consider simpler alternative: when render page delete link_to is, might put or not delete button based on wether there or not loans.

i not speak haml either, i'm pretty sure strightforward include %td= link_to in if ... else ... end statement.

if button should not rendered can write text explains why user cannot delete object. way it's easy user understand situation, rather having click on delete link find out cannot perform action.

cheers,


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -