0

I have a ruby controller file with various methods defined, i want to get response of one of the method in erb i.e frontend using ajax call.

I am a beginner in ruby, so doesn't have much experience of handling requests in ruby, when i try to run the below code it gives error 404 in console stating url not found.

I have added relevant code snippets instead of entire code.


=> appcontroller.rb file


def returnimage_url
  image_url = "http://dfdabfyag.jpg"  //random url
  { :success => true, :img_value => image_url }.to_json
end

=> loadimage.erb file

<script>
function showImage(){
    $.ajax({
            url: '/returnimage_url',
            method: 'GET',
            dataType: "json",
            success: function(response){
                let image_data = JSON.parse(response);
                console.log(image_data);
            }
    });
}

showImage();
</script>
ankita28
  • 3
  • 4

1 Answers1

0

You need to map the path /returnimage_url to your appcontroller.rb file and the specific method.

That should be done in your config/routes.rb file using something like this:

get 'returnimage_url', to: 'app#returnimage_url'

That will work if you rename your appcontroller.rb to app_controller.rb

See routing documentation for more info.

There are more issues in your code, but this should point you in the right direction and resolve the 404.

Siim Liiser
  • 3,860
  • 11
  • 13