My devise.en.yml has the sign_out message
sessions:
signed_in: "Signed in successfully."
signed_out: "Signed out successfully."
and I have a _messages.html.erb to check flash messages
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<%= content_tag :div, msg, :class => "flash_#{name}" %>
<% end %>
<% end %>
and this is my signout button
<%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %>
All my other messages work correctly like signup, signin, etc, etc. The only one that doesnt work is my signout.
Why, and how can I fix it?
Ok, so for the people that was wondering what was wrong, my routing was messed up. after I sign out, the client was trying to access something it shouldn't therefore redirecting to another page while removing the "signed out successfully".
Related
I've a weird problem(I'm still learning rails) with rails and flash messages.
I have a link_to with method post in my rails view:
<%= link_to "<i class='fa fa-times'></i>".html_safe, email_post_path(#post.id), method: 'post' %>
and in my controller I do this:
def email_post
# do nothing
return redirect_to posts_path, alert: 'test flash'
end
The problem
is that works fine(so it redirect me correctly), but don't show the flash message(and the problem is not about the showing flash message, but that the flash[:alert] doesn't exists)
This is the way I show the flash messages:
<%= binding.pry %> #used for see if the flash is empty or not - and in this case is it!
<% flash.each do |type, message| %>
<div class="flash-<%= type %>">
<%= message %>
</div>
<% end %>
Why don't show the flash message in this case ?
Ok the problem seems to be
error: 'lol'
With:
alert: 'lol'
Works fine...why ?
I came across an interesting problem when I try to alter what gets display if page is certain route'
<body>
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
<!---both logged_in and is_admin are in session_helper-->
<%if current_page?(login_path) or current_page?(signup_path)%>
<p class = "title">CLOUD SOLAR</p>
<% else %>
<% if logged_in?%>
<% if is_admin?%>
<%= link_to "All Users", users_path%>
<% end%>
<%= link_to "Settings", edit_user_path(current_user)%>
<%= link_to "Log Out", logout_path, method: "delete"%>
<% else %>
<%= link_to "Sign Up", signup_path%>
<%= link_to "Log in", login_path%>
<% end %>
<% end %>
<%= yield %>
<!-- <%=debug(params) if Rails.env.development?%> -->
</body>
In the code above, I am attempting to get rid of the "sign up" and "log in" link if the current page is either sign up or log in. The above code works fine until I test it with an incorrect email and password. When I test it with a wrong password, a flash message pops up (which it should!), but for some reason the Sign Up and Log in link also pops up underneath it also.
Given the code above, I thought that if the current route is login_path or signup_path, it will never get into the else part of the condition, but somehow it did.
What is going on here? Is the route somehow being altered when I input wrong information even though the url still displays localhost:3000/login or localhost:3000/signup ??
Edit: For clarification, the title disappears completely if the login form has some sort of error. What is replaced is a new login-form and a flash error message. Where did the title CLOUD SOLAR go?
You could try changing login_path and signup_path to login_url and signup_url. Another option would be to hardcode the paths.
current_page?(Rails.env.production? ? 'http://your_live_url/signup' : 'http://localhost:3000/signup')
Also, you could check the documentation here:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-current_page-3F
I am using devise for log in and i have overwrite my devise session controller and showing flash messages if user enter wrong information using following code to display flash message.
<% flash.each do |name, msg| %>
<%= content_tag :section, msg, :id => "flash_#{name}", :class => " flash" %>
<% end %>
And it is working fine and showing "Invalid email or password." message if user enter wrong email and password .
Now it is still showing the same flash message if i refresh the page.
But i want the flash message to be remove if page gets refresh .please help me on same.
You can use flash.discard after it getting displayed. Possibly like:
<% flash.each do |name, msg| %>
<%= content_tag :section, msg, :id => "flash_#{name}", :class => " flash" %>
<% flash.discard(name) %>
<% end %>
I haven't tested this, so try if it works.
In devise.en.yml,i kept signed_out message empty as
sessions:
signed_out: ""
i am having a condition where if failed attempt > 3 then we have to make user signed out.
For this i wrote in the controller as:
if params[:failed_attempt].to_i > 3
current_user.update_attribute(:status, false)
redirect_to destroy_user_session_path(#user), :notice =>
"locked"
end
I am able to update attribute and able to logout but couldnot able to display notice or flash message. Please try to help me out.
I am getting all the messages except this. I also tried :
redirect_to destroy_user_session_path(#user)
flash[:notice] = "locked"
But no use.
Most likely you are setting the flash variable but not actually rendering out the content... try this:
In your application.html.erb before the <%= yield %> paste in code like this:
<% flash.each do |name, msg| %>
<div data-alert id="flashes" class="alert-box <%= name %>">
<%= content_tag :div, msg, id: "flash_#{name}" %>
×
</div>
<% end %>
You can style it afterwards whatever way you want
This done the trick. I just changed my signout as:
sign_out(current_user), :notice =>"locked"
instead of
redirect_to destroy_user_session_path(#user), :notice => "locked"
A am new to devise, and just set it up with default functionality.
I cannot get the flash[:notice] messages to show up.
I put this in my application.html.erb file
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>
I can get the error messages like "already logged in" but I cannot get the regular notice messages like "Signed in successfully."
Can anyone point me in the direction of why this is not working.
I just want to use the defaults I dont care about having custom notices.
Try the change below.
<%= content_tag :div, msg, :id => name %>