问题
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 a warning message when job.price
is missing, asking the user whether to continue or not. Then, it should be redirected to the invoice page only if user chose "yes".
What is the easiest way to implement this ?
回答1:
This worked for me:
link_to("Invoice", "/jobs/#{job.id}/invoice",
:confirm => (job.price ? '' : "Job Price is missing. Continue anyway ?"))
回答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!
回答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.
来源:https://stackoverflow.com/questions/5240960/rails-3-how-to-display-a-warning-message-when-a-link-is-clicked