Rails - 动态切换 link_to 中的确认消息

发布于 2024-11-08 22:04:26 字数 1489 浏览 1 评论 0 原文

我面临着这种丑陋的重复,以便在我看来显示不同的确认消息。

<% 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>

I am facing this ugly duplication in order to display different confirmation message in my view.

<% 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 %>

I would be pleased to know if there is a way to avoid this?

Thank you!

EDIT:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亚希 2024-11-15 22:04:26

只需执行:

<%= 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" %>

或者如果您这样做更容易理解:

<% confirm_message = 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?' %>

<%= link_to "Disconnect #{service.provider.capitalize}", service, :confirm => confirm_message, :method => :delete, :class => "remove" %>

我正在使用 Ruby 三元运算符,请检查它: http://invisibleblocks.wordpress.com/2007/06/11/rubys-other-ternary-operator/

Just perform a :

<%= 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" %>

Or if you this to be more understandable :

<% confirm_message = 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?' %>

<%= link_to "Disconnect #{service.provider.capitalize}", service, :confirm => confirm_message, :method => :delete, :class => "remove" %>

I am using Ruby ternary operator, check it : http://invisibleblocks.wordpress.com/2007/06/11/rubys-other-ternary-operator/

z祗昰~ 2024-11-15 22:04:26

您可以创建一个辅助函数:

def auth_confirm_delete(current_user)
  if current_user.password.nil? and current_user.services.count == 1
      'Remove this service will delete your account, are you sure?'
  else 
      'Are you sure you want to remove this authentication option?'
  end
end 

然后它在视图中看起来更好:

<%= link_to "Disconnect #{service.provider.capitalize}", service, :confirm => auth_confirm_delete, :method => :delete %>

You could make a helper function:

def auth_confirm_delete(current_user)
  if current_user.password.nil? and current_user.services.count == 1
      'Remove this service will delete your account, are you sure?'
  else 
      'Are you sure you want to remove this authentication option?'
  end
end 

and then it looks better in the view:

<%= link_to "Disconnect #{service.provider.capitalize}", service, :confirm => auth_confirm_delete, :method => :delete %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文