1

I have a json object.I want to create a new student by passing this value.How to pass the value of that object to controller using ajax in ruby on rails? This is the code i used for passing the object.

self.save = function() {
       var dataToSave = {
                    Name: self.firstName(),
                    _age: self.Age()
                 }
       alert(JSON.stringify(dataToSave))
         $.ajax({
        url: '/students/new',
        dataType: 'json',
        type: 'PUT',
        data: {total_changes: JSON.stringify(dataToSave)},
        success: function(data) {
            alert("Successful");
          },
          failure: function() {
            alert("Unsuccessful");
          }
        });
       // TODO send request
  };

}

i There is some error in terminal. It shows

Parameters: {"id"=>"new", "total_changes"=>"{\"Name\":\"hu\",\"_age\":\"7\"}"}

id is taken as new.Rake routes

[nithinv@ast297 jquery_country]$ rake routes
     students GET    /students(.:format)          students#index
              POST   /students(.:format)          students#create
  new_student GET    /students/new(.:format)      students#new
 edit_student GET    /students/:id/edit(.:format) students#edit
      student GET    /students/:id(.:format)      students#show
              PUT    /students/:id(.:format)      students#update
              DELETE /students/:id(.:format)      students#destroy

controller is

def new
    @student = Student.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @student }
    end
  end

How can i go to create function in the controller?

Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84

3 Answers3

4

Try this url:

 alert(JSON.stringify(dataToSave))
     $.ajax({
    url: '/students',
    dataType: 'json',
    type: 'PUT',
    data: {total_changes: JSON.stringify(dataToSave)},
    success: function(data) {
        alert("Successful");
      },
      failure: function() {
        alert("Unsuccessful");
      }
    });

Change url to /students

Okky
  • 10,338
  • 15
  • 75
  • 122
0

It does exactly what you ask it to do - converts JSON object into a valid string representation.

Now you need to parse this JSON string:

How do I parse JSON with Ruby on Rails?

Community
  • 1
  • 1
Anton
  • 3,006
  • 3
  • 26
  • 37
0

I think that your routes.rb is having false routing and that is why you are getting id as new.

It should be:

routes.rb

resources "students" 

match "/students/new" => "students#new"

This will call new action in your students controller. So it depends that what code your new action has in the students controller.

The rest of your code seems to be right. But if you still get error than show the error message that you are getting and also the new action code further.

sjain
  • 23,126
  • 28
  • 107
  • 185
  • Yes it is correct and will call the new action of students controller. – sjain Feb 14 '13 at 11:45
  • I edited my code with your suggestion but still the terminal shows Parameters: {"id"=>"new", "total_changes"=>"{\"Name\":\"yut\",\"_age\":\"6\"}"} id= 'new'.... – Nithin Viswanathan Feb 14 '13 at 11:47
  • also there was an error like this ActiveRecord::RecordNotFound (Couldn't find Student with id=new): – Nithin Viswanathan Feb 14 '13 at 11:50
  • show your routes for students by running `rake routes |grep students` – sjain Feb 14 '13 at 12:14
  • [nithinv@ast297 jquery_country]$ rake routes students GET /students(.:format) students#index POST /students(.:format) students#create new_student GET /students/new(.:format) students#new edit_student GET /students/:id/edit(.:format) students#edit student GET /students/:id(.:format) students#show PUT /students/:id(.:format) students#update DELETE /students/:id(.:format) students#destroy – Nithin Viswanathan Feb 14 '13 at 12:16
  • I added the new action of students controller – Nithin Viswanathan Feb 15 '13 at 04:44