I have an instance variable, @source_code in my Rails controller that I want to retrieve in my Ajax response via the success function. I am calling this Ajax function from my index.html.erb file and it renders a show.html.erb file. I want to get my text area to print out the @source_code.code value.
SourcesController.rb
def show
Rails.logger.debug("REACHED: show >>")
@source_code = Source.find_by(id: params[:id])
Rails.logger.debug("REACHED: source_code >>" + @source_code.code.to_s)
@sources = Source.all
end
index.html.erb
function updateTextArea(source_id){
console.log(source_id);
$.ajax({
type: "GET",
url: "/sources/" + source_id,
data: {source_id: source_id},
success: function (response){
alert("Ajax success")
console.log("<%= @source_code %>");
editor.session.setValue("<%= @source_code %>");
},
error: function(){
alert("Ajax error!")
}
});