Rails - 动态切换 link_to 中的确认消息
我面临着这种丑陋的重复,以便在我看来显示不同的确认消息。
<% if current_user.password.nil? and current_user.services.count == 1 %>
<%= link_to "Disconnect #{service.provider.capitalize}", service, :confirm => 'Remove this service will delete your account, are you sure?', :method => :delete %>
<% else %>
<%= link_to "Disconnect #{service.provider.capitalize}", service, :confirm => 'Are you sure you want to remove this authentication option?', :method => :delete %>
<% end %>
我很高兴知道是否有办法避免这种情况?
谢谢你!
编辑:
ActionView::Template::Error (/Users/benoit/rails_projects/website/app/views/services/index.html.erb:15: syntax error, unexpected ',', expecting ')'
...e this authentication option?', :method => :delete, :class =...
... ^):
12: <% for service in @services %>
13: <div class="service">
14: <%= image_tag "logo_#{service.provider}.png", :class => "left" %>
15: <%= link_to "Disconnect #{service.provider.capitalize}", service, :confirm => current_user.password.nil? and current_user.services.count == 1 ? 'Remove this service will delete your account, are you sure?' : 'Are you sure you want to remove this authentication option?', :method => :delete, :class => "remove" %>
16:
17: <div class="clear"></div>
18: </div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需执行:
或者如果您这样做更容易理解:
我正在使用 Ruby 三元运算符,请检查它: http://invisibleblocks.wordpress.com/2007/06/11/rubys-other-ternary-operator/
Just perform a :
Or if you this to be more understandable :
I am using Ruby ternary operator, check it : http://invisibleblocks.wordpress.com/2007/06/11/rubys-other-ternary-operator/
您可以创建一个辅助函数:
然后它在视图中看起来更好:
You could make a helper function:
and then it looks better in the view: