5) Did you define a method called new in the controller whose view this
is?
I don't know if some of those changes are because I'm using rails 2.3.2
and you are using a 1.x version or not. I would guess that it's
mandatory to state what version of rails you are using when you post a
rails question.
(that assumes the links table has only one field: url)
Apparently when you assign a Link object to the variable @link, rails
will create a record in the database corresponding to the values in the
Link object. If you look at the create method in the file
LinksController.rb, that is what the create method does. This is what I
came up with:
After some more testing, I discovered that you don't have to assign the
Link object to @link. I think that when you create a Model object(e.g.
a Link object) and the Model is hooked up to a database, then as soon as
you create the object, rails inserts a record corresponding to that
object in the table.
That isn't quite right either. You have to save an object for rails to
enter it into a table:
However, rails provides a convenience method, create(), that both
instantiates a new object and inserts a record into the table. create()
takes a hash of name/value pairs as an argument, where each name is a
field in the table.
(Or, you can pass create() an array of hashes, where each hash
represents one record, and create() will insert multiple records at the
same time.)
(that assumes the links table has only one field: url)
Apparently when you assign a Link object to the variable @link, rails
will create a record in the database corresponding to the values in the
Link object. If you look at the create method in the file
LinksController.rb, that is what the create method does. This is what I
came up with:
After some more testing, I discovered that you don't have to assign the
Link object to @link. I think that when you create a Model object(e.g.
a Link object) and the Model is hooked up to a database, then as soon as
you create the object, rails inserts a record corresponding to that
object in the table.
That isn't quite right either. You have to save an object for rails to
enter it into a table:
However, rails provides a convenience method, create(), that both
instantiates a new object and inserts a record into the table. create()
takes a hash of name/value pairs as an argument, where each name is a
field in the table.
(Or, you can pass create() an array of hashes, where each hash
represents one record, and create() will insert multiple records at the
same time.)
I'm not sure that I follow you. Completely bypassing the form would be
nice.
I changed the new method to:
def new
@link = Link.create("url" => params[:url])
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @link }
end
end
and it didn't get written to the database. When I changed it to:
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @link }
end
end
it goes to the form page with the correct URL in the textfield. This
was actually what I originally wanted, but it would be better to bypass
the form entirely.