Forum: Ruby on Rails Rails adding dashes to text field in db

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
28c7ef5ffd77ddcaf495aa389103b764?d=identicon&s=25 Jason (Guest)
on 2007-04-17 14:18
I have a model called notes.  Here's the definition in my schema.rb

  create_table "notes", :force => true do |t|
    t.column "sales_order_id", :integer
    t.column "content",        :text
    t.column "timestamp",      :datetime
    t.column "user_id",        :integer
  end

The model class is as follows:

class Note < ActiveRecord::Base
  belongs_to :sales_order
  belongs_to :user
end

I'm trying to add a note by doing the following in the
sales_order_controller:

def add_note
    da_note = Note.new
    da_note.sales_order_id = params[:so_id]
    da_note.content = params[:note_body]
    da_note.user_id = cookies[:user_id]
    da_note.timestamp = Time.now

    if da_note.save!
      flash[:notice] = 'Note added to Sales Order'
      redirect_to :action => 'show', :id => params[:so_id]
    end
end

This works, but it adds the following text to the content field of every
note:  "-- -".  So if I type in 'test note' and submit, I get '--- -
test note' in the db.  What am I doing wrong here?
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-04-17 15:16
(Received via mailing list)
are you calling debug() in your view anywhere?  or to_yaml?
28c7ef5ffd77ddcaf495aa389103b764?d=identicon&s=25 Jason (Guest)
on 2007-04-17 16:12
Chris Hall wrote:
> are you calling debug() in your view anywhere?  or to_yaml?

No, I tried that at first, but it didn't help anything.

I just now changed

params[:note_body]

to:

params[:note_body].to_s and it worked!

Why does it have to be changed to a string?  Isn't it already a string?
153868096910b6bca47e2a9212c2df56?d=identicon&s=25 Frederick Cheung (fcheung)
on 2007-04-17 16:19
Jason wrote:
>
> params[:note_body].to_s and it worked!
>
> Why does it have to be changed to a string?  Isn't it already a string?

Only you can tell us. Still a breakpoint in there and have a look at the
params hash.

Fred
28c7ef5ffd77ddcaf495aa389103b764?d=identicon&s=25 Jason (Guest)
on 2007-04-17 17:43
Frederick Cheung wrote:
> Jason wrote:
>>
>> params[:note_body].to_s and it worked!
>>
>> Why does it have to be changed to a string?  Isn't it already a string?
>
> Only you can tell us. Still a breakpoint in there and have a look at the
> params hash.
>
> Fred

I'm afraid you've gone over my head with that comment.  What will the
params hash tell me that I don't already know?
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-04-17 17:54
(Received via mailing list)
you can just tail your development log and watch it as you post the
data to the server.  that should indicate where the problem is coming
from.  if you see -- - in your params hash (posted data) then the
problem is in your view.  if it's not, then the problem is in your
controller method
588ab1c0a5610a7e160a3b101abb91e6?d=identicon&s=25 MichaelLatta (Guest)
on 2007-04-17 18:09
(Received via mailing list)
Unfortunately no.  Unlike Smalltalk and many other languages, in Ruby
Symbols are not strings.

The value in params[:note_body] was apparently a symbol not a string.

Michael
28c7ef5ffd77ddcaf495aa389103b764?d=identicon&s=25 Jason (Guest)
on 2007-04-17 18:20
> The value in params[:note_body] was apparently a symbol not a string.

Ok, I guess it was a symbol, but I don't have any idea how that
happened.  Oh well, it works now, at least.

Thanks everybody!
This topic is locked and can not be replied to.