隐藏 html 元素

发布于 2024-12-08 16:53:40 字数 262 浏览 2 评论 0原文

我试图隐藏视图中编辑和销毁操作的链接,除非用户已使用 http 基本身份验证登录。我已在下面附上我的文件。谢谢

查看https://gist.github.com/1272716 控制器 https://gist.github.com/1272712

I'm attempting to hide the links for edit and destroy actions in the view unless the user has logged in using http basic auth. I've attached my files below. Thanks

View https://gist.github.com/1272716
Controller https://gist.github.com/1272712

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

错爱 2024-12-15 16:53:40

您需要保存/存储身份验证结果,然后在视图中有条件地呈现。

控制器

protected

def authenticate
  @authenticated = authenticate_or_request_with_http_basic do |user, password|
    user == "x4556d4s" && password == "fd55sas64x"
  end 
end 

视图

<%= link_to "New Link", :controller => :links, :action => :new %>
<table>
<% @links.each do |link| %>
  <tr>
    <td> <%= link_to '+', up_link_url(link), :method => :put %> <%= link.points %> <%= link_to '-', down_link_url(link), :method => :put %> </td>
    <td> <a href=  "<%= link.url %>"> <%= link.title %></a> </td>

    <% if @authenticated %>
      <td><%= link_to 'Destroy', link, :confirm => 'Are you sure?', :method => :delete %></td>
      <td><%= link_to 'Edit', edit_link_path(link) %></td>
    <% end %>

  </tr>
<% end %>
</table>

You need to save/store the authenticate result, and then render conditionally in your view.

Controller:

protected

def authenticate
  @authenticated = authenticate_or_request_with_http_basic do |user, password|
    user == "x4556d4s" && password == "fd55sas64x"
  end 
end 

View:

<%= link_to "New Link", :controller => :links, :action => :new %>
<table>
<% @links.each do |link| %>
  <tr>
    <td> <%= link_to '+', up_link_url(link), :method => :put %> <%= link.points %> <%= link_to '-', down_link_url(link), :method => :put %> </td>
    <td> <a href=  "<%= link.url %>"> <%= link.title %></a> </td>

    <% if @authenticated %>
      <td><%= link_to 'Destroy', link, :confirm => 'Are you sure?', :method => :delete %></td>
      <td><%= link_to 'Edit', edit_link_path(link) %></td>
    <% end %>

  </tr>
<% end %>
</table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文