7stud -- wrote:
> 7stud -- wrote:
>> 7stud -- wrote:
>>> 
>>> Link.create("url" => params[:url])
>>> 
>>> (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:
>>> 
>>> @link = Link.create("url" => params[:url])
>>> 
>> 
>> 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:
> 
> mylink = Link.new
> mylink.url = "www.somepage.com"
> mylink.save
> 
> 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:

  def new
    @link = Link.new("url" => params[:url])

    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.

Thanks for the help so far.
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to