Skip to content Skip to sidebar Skip to footer

Rails 3: How To Display A Warning Message When A Link Is Clicked?

I have the following link in my Rails 3 application: link_to('Invoice', '/jobs/#{job.id}/invoice') When user clicks it, the application shows an invoice. I would like to display

Solution 1:

This worked for me:

link_to("Invoice", "/jobs/#{job.id}/invoice", 
        :confirm => (job.price ? '' : "Job Price is missing. Continue anyway ?")) 

Solution 2:

Try this

<%= link_to "Invoice", "/jobs/#{job.id}/invoice", :onclick => "#{ "confirm('The job price is missing, are you sure you want to continue?')" if job.price.nil? } %>

Hope that's what you are looking for. Good luck!


Solution 3:

Assuming Prototype.js, the easiest thing would probably be an inline click handler. The built-in :confirm option won't work since you need to check a field on the page.

link_to("Invoice", "/jobs/#{job.id}/invoice", :onclick => "if($F('job_price') == '') confirm('Job price is not set. Are you sure you want to continue?');")

There are probably a hundred more sophisticated ways of handling this, unobtrusively or whatever.


Post a Comment for "Rails 3: How To Display A Warning Message When A Link Is Clicked?"