Johnny Rottenweed
Johnny Rottenweed

Reputation: 337

Ruby on Rails: Updating data problems

Okay so I'm just trying to write some simple code that updates a record given an ID. This is what it looks like.

def updaterecord
    bathroom = Bathroom.find(params[:key])
    bathroom.name= params[:name],
    #bathroom.bathroomtype = params[:bathroomtype],
    bathroom.street = params[:street]
    #bathroom.city = params[:city],
    #bathroom.state = params[:state],
    #bathroom.country = params[:country],
    #bathroom.postal = params[:postal],
    #bathroom.access = params[:access],
    #bathroom.directions = params[:directions],
    #bathroom.comment = params[:commment],
    #bathroom.avail = params[:avail]
    bathroom.save
  end 

The problem is that although I am trying to update each individual attribute they are all getting concatenated to the name field. For example, this code above sets the name field to the name plus the address. I don't know why?

This is what the console looks like if I try to query after doing the update.

  Bathroom Load (0.2ms)  SELECT "bathrooms".* FROM "bathrooms" WHERE "bathrooms"."ID" = ? LIMIT 1  [["ID", 4017]]
=> #<Bathroom ID: 4017, name: "--- \n- ttyt\n- 113 some\n", bathroomtype: "0", street: "113 some", city: "Portland", state: "OR", country: "United States", postal: "97217", lat: #<BigDecimal:1109f2890,'0.4558056E2',12(12)>, lon: #<BigDecimal:1109f27c8,'-0.122677857E3',12(16)>, access: "0", directions: "", comment: nil, created: "2012-06-08 17:19:03.420329", modifed: "", avail: "1", slug: "", source: "Squat">

And this is what the post values look like:

post values = key=4017&name=www&bathroomtype=0&street=7540 N Interstate Ave&city=Portland&state=OR&country=United States&postal=97217&access=0&directions=&comment=<null>&avail=1

Why can't I get each field to update individually? Sorry I'm confused as to what is going on?

Upvotes: 1

Views: 121

Answers (2)

socialsiem
socialsiem

Reputation: 54

it doesn't look like you have correctly urlencoded your post values

a simple

puts params.inspect

or

pp params

should log out the params object. You can also use

render :text => params.inspect

to print it out to your result html

Upvotes: 0

Planty
Planty

Reputation: 112

I think you might an unnecessary comma there at the end of the line.

It should just read:

def updaterecord
    bathroom = Bathroom.find(params[:key])
    bathroom.name= params[:name]
    bathroom.street = params[:street]
    bathroom.save
end

Upvotes: 2

Related Questions