Reputation: 1247
Using rails 4 and ruby 2
Having trouble displaying the flash messages from my controllers. My method looks like this:
def create
@salary_report = SalaryReport.create(salary_report_params)
if @salary_report.save
redirect_to @salary_report
flash[:notice] = "Lönerapporten sparades korrekt!"
puts "salary report saved #{flash[:notice]}"
else
render :new, notice: "Något gick fel när lönerapporten skulle sparas!"
end
end
As you can see, I have added a puts statement printing out the flash notice just to prove that the flash notice is getting generated after the redirect.
The logs look like this after creating a salary report:
Redirected to http://localhost:3000/salary_reports/20
salary report saved Lönerapporten sparades korrekt!
Completed 302 Found in 25ms (ActiveRecord: 9.7ms)
After getting to show view logs:
Started GET "/salary_reports/22" for 127.0.0.1 at 2013-07-24 16:08:42 +0200
Processing by SalaryReportController#show as HTML
Parameters: {"id"=>"22"}
SalaryReport Load (0.5ms) SELECT "salary_reports".* FROM "salary_reports" WHERE "salary_reports"."id" = ? LIMIT 1 [["id", "22"]]
Document Lo ad (0.3ms) SELECT "documents".* FROM "documents" WHERE "documents"."salary_report_id" = ? [["salary_report_id", 22]]
Rendered salary_report/show.html.erb within layouts/application (6.1ms)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 3 ORDER BY "users"."id" ASC LIMIT 1
Completed 200 OK in 62ms (Views: 58.0ms | ActiveRecord: 1.1ms)
In the view, I show the messages with this:
<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="alert alert-<%= name == :notice ? "success" : "error" %>">
<a class="close" data-dismiss="alert">×</a>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<% end %>
I have tried a variety of different ways to write the controller method but nothing seems to help. Very uncertain about what the problem could be.
Upvotes: 6
Views: 10735
Reputation: 579
I've got pretty the same problem due to devise gem and I found a solution with the cookies.
You have to set a cookie with a value before redirecting, and then the controller tests if there is a cookie. If yes, it tests what is its value and display the flash message.
Take a look right there for a complete explanation : flash[:notice] not working with the after_sign_out_path_for - (devise)
Hope it can help you.
Upvotes: 0
Reputation: 597
First off, instead of defining the flash message separately, put it inline with your redirect, just like you're doing with your render line below. This is more of a style change than anything majorly functional:
def create
@salary_report = SalaryReport.create(salary_report_params)
if @salary_report.save
redirect_to @salary_report, notice: "Lönerapporten sparades korrekt!"
else
render :new, notice: "Något gick fel när lönerapporten skulle sparas!"
end
end
As for the reason why it is not displaying, this is most likely an issue with your flash message view code. This is because, as you have tested, the flash hash is being set properly, it's just not being displayed.
Try temporarily replacing your flash message code with the following simpler version. Ensure it's in your application.html.erb near the top, but that's again just styling:
<% flash.each do |name, msg| %>
<%= content_tag :p, msg if msg.is_a?(String) %>
<% end %>
Upvotes: 0
Reputation: 11
try use flash.keep.
Anything you place in the flash will be exposed to the very next action and then cleared out.
In this case flash pass into 2 actions, another controller and a view.
def create
@salary_report = SalaryReport.create(salary_report_params)
if @salary_report.save
flash.keep[:notice] = "Lönerapporten sparades korrekt!"
puts "salary report saved #{flash[:notice]}"
redirect_to @salary_report
else
render :new, notice: "Något gick fel när lönerapporten skulle sparas!"
end
end
Upvotes: 0
Reputation: 38645
You are setting your flash[:notice]
after redirect. Try switching the order of those calls i.e. set the flash message first then redirect second:
def create
@salary_report = SalaryReport.create(salary_report_params)
if @salary_report.save
flash[:notice] = "Lönerapporten sparades korrekt!"
puts "salary report saved #{flash[:notice]}"
redirect_to @salary_report
else
render :new, notice: "Något gick fel när lönerapporten skulle sparas!"
end
end
Upvotes: 5
Reputation: 2257
Try defining flash message in a redirect clause:
def create
@salary_report = SalaryReport.create(salary_report_params)
if @salary_report.save
redirect_to @salary_report, notice: "Lönerapporten sparades korrekt!"
else
render :new, notice: "Något gick fel när lönerapporten skulle sparas!"
end
end
Upvotes: 0