I am trying to add the header to my rails application based on authenticating the user. So here I am checking that if the user has logged in or signed in and then adding login/logout link based on that.
But I am getting the following error:
application.html.erb:16: syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '(' ...roy_user_session_path, method :delete );@output_buffer.safe_
Here's what I have tried:
<% if user_signed_in? do %>
<%= link_to "Log out", destroy_user_session_path, method :delete %>
<% else %>
<%= link_to "login", new_user_session_path %>
<% end %>
How can I resolve this?
If you want to check whether user is signed for every action in the application, you have to put the filter in the application controller. You can do this for a specific controller also.
Devise is the cornerstone gem for Ruby on Rails authentication. With Devise, creating a User that can log in and out of your application is so simple because Devise takes care of all the controllers necessary for user creation ( users_controller ) and for user sessions ( users_sessions_controller ).
By default, session data in Rails is stored via a cookie in the user's browser. It's a nice, simple storage mechanism, but it means that the server has absolutely no “memory” of a given session. This can cause security problems for your application.
First of all remove do
from this line, you don't need that
<% if user_signed_in? %>
Secondly add :
after method
, it's a key value pair
<%= link_to "Log out", destroy_user_session_path, method: :delete %>
Hope that helps!
You are making a syntax error in method delete. Copy the below code
<% if user_signed_in? %>
<%= link_to "Log out", destroy_user_session_path, :method=>'delete'%>
<% else %>
<%= link_to "login", new_user_session_path %>
<% end %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With