I'm currently working on a rails application and looking to refresh/reload the index method of my controller without refreshing the view. For example, here is my index method:
def index
@title = params[:title]
end
And the corresponding html.erb:
<script>
function changeField() {
var newTitle = "Alerts"
$.ajax({
url: 'index?title=' + newTitle,
type: 'POST',
contentType: 'application/json',
data: '',
dataType: 'script',
headers: {
'X-CSRF-Token': '<%= form_authenticity_token.to_s %>'
},
complete: function () {
alert('<%=@title%>');
}
});
}
</script>
<button type="button" onclick="changeField()"></button>
My issue lies in the fact that the @title variable on the view does not update, so I always see the original title instead of the new one.