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"
Related
When calling render The view is not being updated, however, I do notice in the network tab, after the POST request is made to me POST route, it's returning HTML as the response, and the response has my rendered error message. It's just not updating the page. I don't know what to make of that.
In my POST action, I'm forcing this to be called
flash.now[:notice] = responseMessage
render :deactivate_show
Which renders the action:
def deactivate_show
#user = User.find(params[:id])
authorize! :deactivate_user, #user
if current_user.role == "admin"
if #user.broker?
#companyUsers = User.where(parent_id: #user.id)
#properties = Property.where(user_id: #user.id)
.or(Property.where(user_id: #companyUsers.ids))
elsif #user.broker_manager?
# get all agents in company
# exclude current user
# get broker
#companyUsers = User.where(parent_id: #user.parent_id)
.where.not(id: #user.id)
.or(User.where(id: #user.parent_id))
#properties = Property.where(user_id: #companyUsers.ids)
.or(Property.where(user_id: #user.parent_id))
else #user.agent?
#properties = #user.properties
if #user.parent_id?
#companyUsers = User.where(parent_id: #user.parent_id)
.where.not(id: #user.id)
.or(User.where(id: #user.parent_id) )
else
# dealing with owner.
#companyUsers = User.where('role IN (2,4,1)')
.where.not(id: #user.id)
end
end
else
end
end
which renders a view I have that displays the form.
I added to my show view:
<div class="messages">
<% flash.each do |key, value| %>
<div class="hello"><%= value %></div>
<% end %>
</div>
Note the class of "hello".
When I make a POST request, the response in the network tab has the notice:
Rendered from POST request response:
<div class="messages">
<div class="hello">testing errors</div>
</div>
Edit: here's the form:
<div class="form-wrap">
<%= form_with do %>
<%= button_tag( id: 'button--submit', class: 'button button--secondary') do %>
<i class="icon-arrow-right"></i>
<span>Deactivate</span>
<% end %>
<% end %>
</div>
Well the actual answer was something no one could have provided since I didn't share my form. I was using form_with, which apparently does AJAX by default, thus not rendering the view.
Thanks for the help from this question: Rails render not showing in browser, despite positive server reply
I've changed
<%= form_with do %>
to
<%= form_with local: true do %>
and now it works.
The Flash is only rendered upon a new request, which you don't make with render. Instead, you should use flash.now[:notice] to have it display on render.
I am still new to Rails and am having trouble understanding how to render certain sections of a page conditionally. I have a button in index.html.erb as well as another partial rendered:
<%= #facade.processing_button %>
<%= render 'snap', app: #facade.app %>
Which is defined as follows:
link_to processing_path(#app.id),
method: :post, action: :processing,
class: 'btn btn-danger' do
concat([
image_tag('blah', class: 'check_icon'),
content_tag(:span, 'Processing')
].join(' ').html_safe)
end
This button calls a controller method:
def processing
if service.upload
# render success bar?
else
# render error bar?
end
end
I would like to render something like the following pictures. In the snap partial, a section looks like this normally:
Once the button is clicked, if the action is successful I want to render the following green success bar:
It is unclear to me how to accomplish this. Should I be leveraging some form of JS/CoffeeScript? Should I add the bars to the partial as hidden by default and simply show them with JS when the action is completed?
link_to processing_path(#app.id), method: :post, action: :processing Using both _path and :action parameters doesn't make sense. Use only one of them
You need to decide whether your button is doing a "traditional" request or an AJAX request
In case of a traditional request, you can use a controller variable #success = ..., and then check this variable inside the view: <% if #success %>
In case of an AJAX request, things would get a little more complicated. However, there's Rails support for "success" and "failed" AJAX responses. Take a look at https://guides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers. Typically, you would show/hide certain elements on the page, depending on the server response
You would need something like this on your layouts
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, class: "alert alert-info" %>
<% end %>
Then on your controller
def processing
if service.upload
flash[:notice] = "Success"
else
flash[:notice] = "Error"
end
end
Take a look at this: rails 4 -- flash notice
see Doc: https://coderwall.com/p/jzofog/ruby-on-rails-flash-messages-with-bootstrap
Step 1: Add flash code in layouts/application.html.erb file
<% flash.each do |key, value| %>
<div class="<%= flash_class(key) %>">
<%= value %>
</div>
<% end %>
Step 2: Just need to quickly extend application_helper.rb with the following
def flash_class(level)
case level
when :notice then "alert alert-info"
when :success then "alert alert-success"
when :error then "alert alert-error"
when :alert then "alert alert-error"
end
end
# sometimes it will not work then wrap it with single quotes. for example:
when 'notice' then "alert alert-success"
Step 3: Add below in controller.erb
def processing
if service.upload
flash[:success] = "Processing complete!"
else
flash[:error] = "Something went wrong!"
end
end
Hope it will work :)
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 ?
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 %>
So, I'd like to be able to display both a flash[:notice] and a flash[:error] on the same action, but I'd like for the :notice to always be displayed above (or before) the error. Is there a way to do this?
In my controller, I thought I could just code the flash[:error] before the flash[:notice], so that rails will display it correctly, and it does a vast majority of the time. But every now and then they are randomly switched, and I can't seem to figure out why. So, how can I ensure that a flash[:notice] is always displayed above an :error ?
Edit: Thanks to Ben's and Ryan's advice, I've now just set up conditionals in my layout/application file.
<% if flash[:notice] %>
<div id="flash_notice"><%= flash[:notice] %></div>
<% end %>
<% if flash[:error] %>
<div id="flash_error"><%= flash[:error] %></div>
<% end %>
I'm pretty satisfied with this, but maybe there's an even better way?
Edit #2: Yes, there is. Ben added it to his answer below. Thanks again Mr. Ben.
The order of display simply depends on your view code. If you do this in your controller:
flash[:notice] = "Notice me"
flash[:error] = "BAAAAADDDD"
And this in your view:
<p><%= flash[:notice] %></p>
<p><%= flash[:error] %></p>
the :notice will always appear first.
Try this for a more elegant approach. The order you define in the array is the order they'll appear in:
<% [:error, :notice].each do |condition| %>
<%= "<p class='#{condition.to_s}'>#{flash[condition]}</p>" if flash.has_key?(condition) %>
<% end %>
Always nil your notice and error object.
<% if flash[:notice] %><p><%= flash[:notice] %><% flash[:notice]=nil %></p><% end %>
<% if flash[:error] %><p><%= flash[:error] %><% flash[:error] =nil %></p><% end %>
I would make this a comment but it doesn't do multi-lined code snippets very well. They're probably "randomly switched" because you're using it like this:
<% flash.each do |k, v| %>
<div id='flash_<%= k %>'><%= v %></div>
<% end %>
In 1.8 hash keys are unordered and because flash is a Hash, you'll get these output in whatever order Ruby feels like at the time. As Ben suggested, putting them in the "correct" order manually would help.