Rails trouble sending AJAX data to controller
$.ajax({
url : "/messages", //by rails convention doing a post simply to '/messages' should be handled by create action
method : "post",
data : { data_value: x },
dataType : "json", //this might be wrong?
success: function(response){
//do something,maybe notify user successfully posted their message
},
error: function(error){
console.log(error);
}
});
And in your create
method you should handle json
requests
def create
//save message
@message.save!
respond_to do |format|
format.json { json: @message.as_json }
end
end
This is how your code should look like. You can always do more creatively.
Please checkout this gist
Using ajax to send data to ruby controller
You need to do somthing like this:
Whatever is after the 'render json:' will be returned to the ajax call as the result. In this case, if the user saves it returns the words "it works":
def add_song()
if user_signed_in?
session[:user_id] = current_user.id
present_user = User.find(session[:user_id])
present_user.playlist.store(params[:data_value], 1)
if present_user.save
render json: { success: "It works" }
end
end
end
Now on the Ajax call we need to define the callback to handle the response in case of success. You also need to add a class in your view (in this example the id of this class is 'result') and in that element you will render the response you want to show:
...
else if(this.id == 'add_song') {
$.ajax({
url: "adding",
type: "post",
data: {data_value: JSON.stringify(player.src)},
success: function(response) {
$('#result').html(response.success);
}
});
}
Rails Send with AJAX parameters to custom action in controller from the view
Based on the output of your rake routes command, the route you're making a request to is scoped within the admin
scope, so you should accordingly change your JS to:
if ((subject != null && subject != "") && (message != null && message != "") && (representing_id_1 != null && representing_id_1 != ""))
{
url = "<%= admin_send_email_representing_path %>"; //<-- add admin scope here
$.ajax({
Passing variable to controller through Ajax call
How about
#_index.html
javascript:
$('#AuthorsIndex').DataTable({
ajax: '/authors?author=<%= author %>',
columns: [
{title: 'Publish Date', data: 'created_at'},
{title: 'Publisher', data: 'publisher'},
{title: 'Title', data: 'title'},
]
});
#authors_controllers.rb
def index
@authors = Author.where(author: params[:author])
render json: { data: @authors }
end
Use Ajax to request for rails controller data
If everything was working you would do:
success : function(response) {
mysongs = response;
}
The 'response' parameter on the success function is the result of the ajax call you are sending from your controller; the @songs json.
A simple way to debug your javascript (to check what is the value of your 'response' after the ajax request) is to right-click on your browser and click on 'inspect', then go to the 'Sources' tab, look for your javascript file and click the line where you want to add the debug point (in this case the line with 'mysongs = response', then refresh the page. It should stop running at that point and show the value of 'response'.
Related Topics
How to Get the Version from a Gemspec File
How to Run Phantomjs on Heroku
Help Understanding Yield and Enumerators in Ruby
Rails: Pg::Insufficientprivilege: Error: Permission Denied for Relation Schema_Migrations
How to Route Controllers Without Crud Actions
Ruby Convert String to Method Name
Ruby: Build Failed (Macos 11.2 Using Ruby-Build 20210119) MAC Big Sur
What Does Class Classname < ::Otherclassname Do in Ruby
Circular Dependency Detected While Autoloading Constant When Loading Constant
Pry Not Stopping When Called from a Ruby Script That Reads from Stdin
Setting Up Facets in Elasticsearch with Searchkick Gem in Rails 4.1
Ruby Regex - Gsub Only Captured Group
Run Delayed Jobs and Sidekiq at the Same Time