Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9060647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:17:00+00:00 2026-06-16T15:17:00+00:00

I am trying to update a rails model but I am facing some errors

  • 0

I am trying to update a rails model but I am facing some errors with the data handling even though the ajax request does go through.

The parameters being sent appears as shown below:

Parameters: {"{\"slot_allocation\":{\"timeslot_id\":\"3\",\"subject_id\":\"3\",\"slot_allocation_id\":\"1\",\"group_index\
":\"Group 2\",\"lesson_type\":\"Tutorial\"}}"=>nil, "id"=>"1"}
  SlotAllocation Load (0.2ms)  SELECT "slot_allocations".* FROM "slot_allocations" WHERE "slot_allocations"."id" = ? LIMIT 1
  [["id", "1"]]

Whereas a normal update does:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"yBib4ftNdB/r25uZuGnqEZI+r9dulVhyoce8nWBfcBQ=", "slot_allocation"=>{"slot_
allocation_id"=>"1", "lesson_type"=>"Lecture", "group_index"=>"Group 1", "timeslot_id"=>"1", "subject_id"=>"1"}, "commit"=>"
Update Slot allocation", "id"=>"1"}
  SlotAllocation Load (0.3ms)  SELECT "slot_allocations".* FROM "slot_allocations" WHERE "slot_allocations"."id" = ? LIMIT 1
  [["id", "1"]]

I am not sure where I have gone wrong being new to Rails and JSON.

I have tried out solutions posted by How do I parse JSON with Ruby on Rails?. But after changing my codes the requests does not get sent out. I have also tried changing the data being sent by following certain examples but it didn’t work out as well.

I would appreciate advice on what I could do to solve this error.

The following are the relevant codes:

My Ajax request:

var url = 'slot_allocations/'+alloc_id;

var dataToServer = {"slot_allocation":{"timeslot_id":timeslot, "subject_id":subject,"slot_allocation_id":alloc_id, "group_index":"Group 2", "lesson_type":"Tutorial"}}

  $.ajax({
    type: "PUT",
    url: url, 
    dataType: "json",
    data: JSON.stringify(dataToServer) 
    });

My controller update action:

  def update
    @slot_allocation = SlotAllocation.find(params[:id])

    respond_to do |format|
     if @slot_allocation.update_attributes(params[:slot_allocation])
        format.html { redirect_to @slot_allocation, notice: 'Slot allocation was successfully updated.' }
        format.json { head :ok}
      else
        format.html { render action: "edit" }
        format.json { render json: @slot_allocation.errors, status: :unprocessable_entity }
        format.js  { render :js => @slot_allocation.errors, :status => :unprocessable_entity }
      end
    end
  end

I would appreciate any advice on where I might have gone wrong. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:17:01+00:00Added an answer on June 16, 2026 at 3:17 pm

    update_attributes expects a Hash. By converting the data you send to the server to JSON, params contains a JSON string instead. You should call the controller with standard POST data:

    $.ajax({
      type: "PUT",
      url: url, 
      dataType: "json",
      data: dataToServer
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to update a model on a callback but the validation is causing
I am trying to model a very simple system, but the Rails way of
I am trying to update a nested has_one model using mongoid but it will
Im trying to update an sqlite table row(id, data columns) from rails console. All
In my Rails application I'm trying to update a model's attribute using update_attribute in
I'm trying to create/update a location while saving a checkin in my Rails project,
I am trying to implement a bulk update feature in a Rails app, the
Im trying to update the contents of an element after running some php code.
I'm trying to write a Ruby class that works similarly to Rails AactiveRecord model
I am trying to update a model through JSON. Right now I am able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.