隐藏html元素

时间:2011-10-09 20:26:55

标签: html ruby-on-rails ruby ruby-on-rails-3 model-view-controller

我试图在视图中隐藏编辑和销毁操作的链接,除非用户使用http basic auth登录。我在下面附上了我的文件。感谢

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

1 个答案:

答案 0 :(得分:1)

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

<强>控制器

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>