Why the `flash[:warning]` message is not displayed? - ruby-on-rails

I am using Rails 3.1.0 and I would like to understand why the following code in a controller action doesn't properly display the flash[:warning] message even if I state a <%= content_tag( :div, flash[:warning]) %> in the application.html.erb file.
flash[:warning] = "Warning message!"
respond_to do |format|
format.html { redirect_to :root }
end
Why the flash[:warning] message is not displayed? How can I display that after redirection?
P.S. I: I tryed to use flash.keep[:warning] but that didn't work.
SOLUTION
The problem is that I am redirecting two times in my controller actions.
P.S. II: Who voted down can at least give some reasons...

flash.now[:key] will save flash message again key for one request try it
flash.now[:warning] ='message'
and then after redirection show warning message with
<%= flash.now[:warning] %>

Have you written this line in your layout/view? If not then write this code in your layout file
<%= flash[:warning] || flash[:notice] || flash[:alert] %>

Related

How do I get flash[:notice] to display in my view?

I'm having difficulty getting my notices to display in my Rails 5 view. I have this before_filter method set up in a controller:
def check_email_confirmed!
redirect_to controller: "users", action: "edit", notice: 'Please confirm your email.' unless current_user.email_confirmed
end
and in my view I have this
<% if flash[:notice] %>
<p class="flash-notice"><%= flash[:notice] %></p>
<% end %>
but despite the fact that I know the notice is getting set (I see it in my URL) the above is not getting invoked in my view (I see no HTML with the class="flash-notice").
Is there some other way I should be setting flash notices in a redirect? Or should I be using a redirect at all (someone told me there might be some security risks in embedding messages in the URL query string)?
You're currently setting a parameter with the key notice, not setting the flash in your session.
To accomplish this the way you're doing it you would have to do:
<p class="flash-notice"><%= params[:notice] %></p>
Most Rails apps that I've worked on set the session[:flash] in the controller method, in which case you would do:
unless current_user.email_confirmed
redirect_to edit_user_path(current_user)
flash[:notice] = 'Please confirm your email.'
end
Unless you have a good reason to pass the notice text as a URL param, I'd recommend doing it this way.
Try
flash[:notice] = 'bla'
redirect_to controller: "users", action: "edit"
I'm pretty sure that
redirect_to edit_user_path(user), notice: 'bla'
Will also work. For some reason your syntax apparently doesn't pick up the notice modifier. But you'll have to assign user to your current user for that to work obviously.

rails 4, nested resource doesn't updates without page refreshing, using Ajax

So, I'm having Events model which has_many Questions. Relation is the same as in Post->Comment. I've tried to implement some Ajax for Questions, but it seems like I'm missing something, because View doesn't updates without refreshing the page. The same problem with delete method.
Here's my question_controller.rb:
def create
#question = #event.questions.create(question_params)
#question.user_id = current_user.id if current_user
if #question.save
respond_to do |format|
format.html { redirect_to event_path(#event) }
format.js # render questions/create.js.erb
end
else
render 'events/show'
end
end
Question _form.haml:
= simple_form_for [#event, #event.questions.new], remote: true do |f|
= f.input :content, label: 'Your question:', id: 'question_content'
= f.submit class: 'submit_btn'
Question partial _question.haml:
%h4
= question.content
This is how it looks in Events show.haml:
%section#question_section
%h1 Questions:
#questions
= render #event.questions
#question-form
%h1 Submit a question:
= render 'questions/form'
And create.js.erb file:
$('#questions').append("<%= j render partial: #question %>");
$('#question_content').val('');
I've tried few different tutorials, but always had the same problem. View updates only after refreshment. Also it is my second post on Stackoverflow so would be grateful for any suggestions if something is wrong with it.
EDIT 1: mostly I've been following this tutorial: https://pragmaticstudio.com/blog/2015/3/18/rails-jquery-ajax
EDIT 2: create.js.erb seems not to respond, tried to test it with alerts or console log, but without any success. After triggering a button in the console having: "Processing by QuestionsController#create as JS" (so it runs a proper file, but file itself doesn't executes any code). Have no idea why that happening.
So the problem was because of HAML. Had to change my format.js line in the controller to:
formta.js { render layout: false }
Here's the thread that helped to solve this: Rails update.js.erb not executing javascript

Flash message not working on Rails 4 after a redirect

def show
email_address = EmailAddress.find_by(confirmation_token: params[:id])
if email_address.confirm!
flash[:notice] = "Thanks for confirming your email address (#{email_address.email})"
flash.keep
redirect_to user_account_path
else
flash[:error] = "Invalid confirmation token"
redirect_to root_path
end
end
Trying to redirect from this controller to another controller and i want to flash "Thanks for confirming your email address (#{email_address.email})" but am not seeing the flash message on the page am redirecting to.
Tried hardcoding a flash message on the other controller and it got displayed.
It's only when I do a redirect the flash message doesn't go through.
You can use redirect_to and flash together:
redirect_to user_account_path, notice: "Thanks for confirming your email address (#{email_address.email})"
Try to use this way to make redirect and show flash notice.
If this is doesn't work then probably your problem related to rendering flash message. Check content of flash helper in view and code which render it.
You might be missing flash code in layout.
Add the following in layouts/application.html.erb in body section:
<% flash.each do |name, msg| -%>
<%= content_tag :div, msg, class: name %>
<% end -%>

Notice is Firing Twice when Saving User Model

When I save a new user the notice fires twice. Any suggestions on how to get it to fire once? I am also using Sorcery
def create
#user = User.new(user_params)
if #user.save
redirect_to #user, notice: 'Profile successfully created.'
auto_login(#user)
else
render :new
end
end
As per the code snippet given above, I don't think notice will appear twice. There is nothing wrong in your code. There is one possible chance that in your application layout displaying notice is defined once and in your current view template it's defined again. That's why it's showing twice.
Flash:
The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed to the very next action and then cleared out.
A notice can't fire twice according to your code. In your app you somewhere have this code twice (my guess would be in your layout file and then your view)
<% flash.each do |name, msg| %>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<% end %>

Flash notice with redirect_to is broken in rails

I updated to Rails 2.3.10, Rack 1.2.1 and now none of my flash messages are showing up. I found that during a redirect the notice is passed in like this
redirect_to(#user, :notice => "Sorry there was an error")
And in my view the flash hash is empty
<%= debug flash %>
!map:ActionController::Flash::FlashHash {}
But you can see the message in the controller. What gives?
<%= debug controller.session %>
session{:home_zip=>"94108", :session_id=>"xxx", :flash=>{:notice=>"Sorry there was an error"}, :user_credentials=>"1baf9c9c0423ce0151ec32e24cc422f07309e4ba503eb4830635ecc115da217809997324374bb273b3fb792895c9741a8b8c9ea4267771a1bd149de5b9179ea0", :user_credentials_id=>22, :home_market_id=>11}
Edit Profile
Did you check the rails bug tracker? I still use the old fashioned setter flash[:notice] = message and it works fine, so it seems to be a redirect_to method problem.
https://rails.lighthouseapp.com/
Did you try redirect_to url, :flash => { :notice => "notice" }, as a work around?
The code below should work:
redirect_to(#user, {:notice => "Sorry there was an error"})
I'm guessing this is due to changes in Ruby and not in Rails, because it looks like a token parsing priority change in the compiler.
We just ran into this too. All our flash messages disappear with redirect, but not when set in the controller explicitly.
Does not work:
def create
if #obj.save
flash[:notice] = "The #{cname.humanize.downcase} has been created."
redirect_back_or_default redirect_url
else
render :action => 'new'
end
end
This does work:
def show
#user = current_user
flash[:notice] = "Hello -- this will show up fine"
end
This could be an issue with cookies. Long story short, cookies don't get if you redirect immediately afterwards. Assuming that Rails implements flash using cookies, the redirect is your problem.
Sources:
http://persistall.com/archive/2008/01/25/cookies--redirects--nightmares.aspx
http://stackoverflow.com/questions/1621499/why-cant-i-set-a-cookie-and-redirect

Resources