Cisco Ri wrote:
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.
Sorry, I just started with rails, and modifying the scaffold generated
files like you did seems to interfere with the complex web of
relationships. Also, any attempt I made to call one of the methods in
the LinksController directly, e.g.
http://localhost:3000/links/create
gave me a routing error. That is probably for security reasons.
Because you don’t care about the form page, why not bypass it
altogether? The form page is for gathering the data from the user and
assembling it into a url and then calling the url. Your user is going
to assemble the url themselves and call the url by themselves–by
entering the url in their browser’s address bar. So you can tell the
user to assemble a url that calls one of your own methods(=an action),
which then enters the link in the links table.
Here’s something that worked for me:
- Create a new controller and action that the user will enter as the
url:
$ ruby script/generate controller direct entry
That creates a controller called DirectController with one action(=a
method) called entry. Open up app/controllers/direct_controller.rb and
you will see this:
class DirectController < ApplicationController
def entry
end
end
The name/value pairs in the url after the ? will be inserted into a
“params” hash, which you can access. With the information in params,
you can create a new Link object like this:
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])
Add that line to the entry method:
class DirectController < ApplicationController
def entry
@link = Link.create(“url” => params[:url])
end
end
Then when you enter the url:
http://localhost:3000/direct/entry?url=www.someurl.com
that will access the DirectController and call its entry method. The
entry method then creates a new Link object. Then rails enters a record
in the links table corresponding to the information in the Link object.
I have no idea whether that is the proper way to do things, and I can
see at least one issue: a GET request should not make any changes on the
host.