Rails 3: How to display a warning message when a link is clicked?

佐手、 提交于 2019-12-14 03:16:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!