Submit form through GET requets

Cisco Ri wrote:

Right now my controller looks like:

<% form_for @link, :method => :get do |f| %>

1) Controller? Isn't that a View?

2) Looking at an example in my book for form_for shows that the first argument for form_for is a Symbol, e.g. :link.

3) You didn't specify the action(a method in the controller) that you want the form data to get sent to, for instance:

<% form_for :link,             :url => {:action => :new} ....... %>

Since a rails url is of the form:

host/controller/action

and your url is:

http://localhost:3000/links/new?url=http://www.google.com

you are trying to call an action(or method) named new that is defined in the links controller. That is why I specified :new for the action.

4) My book shows specifying the :method a different way:

<% form_for :link,             :url => {:action => new},             :html => {:method => :get} ....... %>

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.

When I use <% form_for :link,             :url => {:action => :new},             :html => {:method => :get} do |f| %>

the type http://www.gmail.com into the form and hit submit, the address bar changes to http://localhost:3000/links/new?link[notspamurl]=http%3A%2F%2Fwww.gmail.com&commit=Create (remove notspam) and nothing gets added to the database.

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.)

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.