Hiding html elements
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 be开发者_高级运维low. Thanks
View https://gist.github.com/1272716 Controller https://gist.github.com/1272712
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>
精彩评论