Milos Radosavljevic Milos Radosavljevic - 3 months ago 12
Ruby Question

Redirect doesn't show notice

I want to when i create an object to redirect the page to index and show a text.

The default that doesnt redirect the page is :

format.html { redirect_to @todo_item, notice: 'Todo item was successfully created.'}


so i changed it to redirect to index like this :

format.html { redirect_to action: :index, notice: 'Todo item was successfully created.' }


but now it does not show the text in the notice, can someone tell me why and how can i fix it?

p.s. if you give me a MINUS please explain in comment what i did wrong so i can improve myself

the code that displays notice in index.html.erb is

<p id="notice"><%= notice %></p>

Answer

The trouble is if the first argument is a hash entry like action: :index then any other hash entries are assumed to be part of the same hash and so part of the first argument, not the response_status that should be in the second argument.

You can fix this by explicitly showing that the notice is a separate hash...

format.html { redirect_to({ action: :index }, { notice: 'Todo item was successfully created.'}) }