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.
Jason (Guest)
on 2007-04-17 16: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?
Chris H. (Guest)
on 2007-04-17 17:16
(Received via mailing list)
are you calling debug() in your view anywhere?  or to_yaml?
Jason (Guest)
on 2007-04-17 18:12
Chris H. 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?
Frederick C. (Guest)
on 2007-04-17 18: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
Jason (Guest)
on 2007-04-17 19:43
Frederick C. 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?
Chris H. (Guest)
on 2007-04-17 19: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
MichaelLatta (Guest)
on 2007-04-17 20: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
Jason (Guest)
on 2007-04-17 20: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.