I'm currently working on implementing an ajax post request feature in my project. The goal is to have a button on my html page trigger a javascript event listener, which then initiates an ajax post request to receive some text data. However, I seem to be facing difficulties with getting the success function in my ajax request to display the received text. I suspect that the issue lies with how my controller.rb file is handling the request. Here is what I have so far:
index.html.erb
<%= javascript_include_tag "application" %>
<h1>Welcome to my website#index</h1>
<input class="className" id="idName" type="button" value="Click Me!" id="my_button" remote:true/>
<ul id="ulID">
<div class = 'wantToChange'>This should change when button is pressed.</div>
</ul>
application.js
$(document).ready(function(){
$('.className').on('click', function(){
console.log("inside js click listener");
$.post('superman', { field1: "hello", field2 : "hello2"},
function(returnedData){
console.log(returnedData);
});
});
});
welcome_controller.rb
class WelcomeController < ApplicationController
def index
end
def superman
some_parameter = params[:some_parameter]
#do something with some_parameter and return the results
render plain: "OK"
end
end
routes.rb
Rails.application.routes.draw do
get 'welcome/index'
resources :test do
collection do
get 'test'
end
end
root 'welcome#index'
end
When I click on the button, an error is generated: POST http://localhost:3000/superman 404 (Not Found) I suspect there may be an issue with how my controller file is configured to receive the 'superman' route. Any assistance on this matter would be greatly appreciated. Thank you.