jason
April 17, 2007, 2:18pm
1
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?
jason
April 17, 2007, 3:16pm
2
are you calling debug() in your view anywhere? or to_yaml?
jason
April 17, 2007, 4:12pm
3
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?
jason
April 17, 2007, 4:19pm
4
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
April 17, 2007, 5:43pm
5
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?
jason
April 17, 2007, 6:09pm
6
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
April 17, 2007, 5:54pm
7
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
jason
April 17, 2007, 6:20pm
8
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!