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 6933031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:46:48+00:00 2026-05-27T11:46:48+00:00

I have a Rails RESTful web service application that accepts a value from a

  • 0

I have a Rails RESTful web service application that accepts a value from a client to increment the value in the database. The database value is an integer, but when using rspec to test the code, the value being passed in is interpreted as a string.

I’m using Rails 3.1 and Ruby 1.9.2.

Here’s the rspec snippet:

...
it "should find Points and return object" do
  put :update, :username => "tester", :newpoints => [10, 15, 0], :format => :xml
end
...

Here’s the controller code:

...
respond_to do |format|
  if points.update_attributes([xp + :newpoints[0]][sp + :newpoints[1]][cash +        :newpoints[2]])
    format.json { head :ok }
    format.xml { head :ok }
...

xp, sp and cash are values from the database and have been validated as Fixnum datatype. The error I am getting is:

TypeError: String can't be coerced into Fixnum

How do I write my test to ensure that the parameters being passed are passed as the proper datatype?

I can include more of the code if needed. Thanks in advance!

  • 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-05-27T11:46:48+00:00Added an answer on May 27, 2026 at 11:46 am

    It took me a bit of head banging, but I found out that I was passing everything in wrong. The solution I’ve come up with is definitely not the best solution and could probably be re-written, but it works and for now, that’s sufficient.

    The change to the rspec snippet was to create hash represented by symbol :newpoints

    it "should find Points and return object" do
      put :update, :username => "tester", :newpoints => {"experience_points" => 10, "shame_points" => 15, "gold" => 0}, :format => :xml
    end
    

    The handling of this request in the controller required a bit of tweaking, but here’s the pertinent pieces:

    class PointsController < ApplicationController
      #before_filter :authenticate, :only => :update
      before_filter :must_specify_user
      before_filter :fix_params
      before_filter :clean_up
      respond_to :html, :xml, :json
    
      def fix_params
        if params[:points]
          params[:points][:user_id] = @user.id if @user
        end
      end
    
     def clean_up
       @newpoints = params[:newpoints]
       @experience = @newpoints["experience_points"]
       @shame = @newpoints["shame_points"]
       @gold = @newpoints["gold"]
       @xp = @experience.to_i
       @sp = @shame.to_i
       @cash = @gold.to_i
    end
    
    def update
      points = Points.find_by_user_id(@user.id, params[:id])
      xp = points.experience_points
      sp = points.shame_points
      cash = points.gold
      final_experience = xp += @xp
      final_shame = sp += @sp
      final_gold = cash += @cash
      final_points = {:experience_points => final_experience, :shame_points => final_shame, :gold => final_gold}
      if_found points do
        respond_to do |format|
          if points.update_attributes!(params[final_points])
            format.json { head :ok }
            format.xml { head :ok }
          else
            format.json { render :nothing => true, :status => "401 Not Authorized"}
            format.xml { render :nothing => true, :status => "401 Not Authorized"}
          end
        end
      end
    end
    end
    

    Obviously, much can be done to make this follow DRY and what not, so any suggestions are still welcome. Thanks in advance!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails web app that is used to gather data from an
I have a RESTful web application that supports multiple sort fields on a collection
I have a rails application where each user has a separate database. (taking Joel
I have a Rails application using Restful authentication and declarative authorization. I have some
I have a rails site that allows authentication through OAuth (I use Restful Authentication
I have a RESTful Rails application with a resource called Foo. I'm trying to
I have a Rails application that right now is pretty standard: Heroku/PostgreSQL backend, users
A number of Rails 2.3 apps are using Restful Authentication but that plugin seems
My goal is to build a standalone RESTful Rails 3 service that communicates with
I have a standard restful rails application. format.html { @users = User.find(:all, :limit =>

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.