I am playing around with render/redirects in my rails application, i have the following controller action
class MessagesController < ApplicationController
def create
@message = Message.new(body: params[:message])
if @message.save
head :created
else
flash[:notice] = "failed!"
render "home/index"
end
end
here is my home/index.html.erb file
<% if flash[:notice] %>
<div class="notice"><%= flash[:notice] %></div>
<% end %>
<form>
<textarea name="message" id="message" placeholder="Type your message..."></textarea>
<input type="submit"></input>
</form>
I am submitting the form with jquery:
$(document).ready(function(){
$('form').submit(function(e){
e.preventDefault();
var message_body = $('#message').val();
$('#message').val('');
$('#message').focus();
$.ajax({
url: '/messages',
method: 'post',
data: {message: message_body}
});
});
});
I have a validation in my form model that ensures that the message body cant be blank validates :body, presence: true
So when i create an empty message and hit submit, it renders the home/index template. (as it should)
Inside chrome, in the preview/response tabs of dev console i can see the flash message, however in my actual application, i cant see the flash notice until i refresh the page.. then it shows up.
Am i mis-understanding something about the ways pages are rendered/the differences between render/redirect_to or is this a turbo links issue ? I tried removing turbo links by removing //= require turbolinks from my application.js file and restarting my server.. still the same issue .
Any ideas ?