- Simple approach:
On top of your form page, have a div to store the message:
In your request handler for the remote request in the controller, compute the message to show.
def create
...
@message = get_message
...
end
Since it is an ajax request, your view will be a .js.erb file (assuming you are using erb). In that view, just set the message div content as @message:
# create.js.erb
$('#message').text("#{@message}");
- Advanced approach:
Alternatively, if you want richer formatting, create a partial for the message formatting and render it in the js.erb file.
For example, I use bootstrap for my styles, and I can make the message look like an alert with an 'x' to close it - so I create a partial to render the alert:
# views/home/_message.html.erb
<div class='alert alert-warning alert-dismissible'>
<%= message %><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
And in your js.erb you could do this instead,
# create.js.erb
$("#message").html("<%= escape_javascript(render partial: 'home/message', locals: { message: @message } ) %>");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…