1

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!")
    }
 });

2 Answers 2

3

Expanding on Nycen's answer, you first want your controller handle the ajax request and return a JSON response:

def show
  respond_to do |format|
    format.json { render json: Source.find_by(id: params[:id]) }
  end
end

PS: Take care with that, it will send all of the fields of your Source record down the wire. I call slice (see ActiveRecord.slice()) on the model to limit the fields returned in the JSON.

Then your JavaSript needs to use the JSON result of that ajax call:

function updateTextArea(source_id){
  console.log(source_id);
  $.ajax({
    type: "GET",
    url: "/sources/" + source_id,
    success: function (response){
      alert("Ajax success")
      console.log(response.code);
      editor.session.setValue(response.code);
    },
    error: function(){
     alert("Ajax error!")
    }
 });

It depends on how your routes are setup, but there should be no need to set the data property in the Ajax call. Your route is likely to pull it from the URL path: /sources/12345.

Note there is no show.html.erb with this setup. There is no view, your controller just returns JSON.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! This really helped clarify Nycen's comments further, and it is exactly what I was looking to do.
Awesome, I could not write code easily at the time of my answer. I would probably rename "response" to "source" to make it more explicit, but that's just me.
1

You're expecting your success handler to have access to @source_code just like a "show.html.erb" view would, but it doesn't work that way.

When you use ajax, the method is called from the browser; it's a piece of code you send away from your server, it can still interact with it, but it doesn't have access to the controller variables.

So, your show action needs to render something your handler can understand, for instance json. Then you'll have access to it in your success by reading your "response" variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.