Cisco Ri wrote:
>>
>> Completely bypassing the form would be 
>> nice.
>> 

Did you try this:


> Here's something that worked for me:
> 
> 1) 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'm not sure that I follow you. 

If you create a Link object, then call .save on it, the information in 
the Link object will be automatically inserted in the links table. 
There are two ways to create and save a Link object:

1)
    mylink = Link.new
    mylink.url = "some url"
    mylink.save

You create the Link object, then you set the field names--as found in 
the links table--e.g. 'url', to the desired values.

2)  Link.create(info)

where info is a hash containing the information, e.g.:

{"field1" => val1, "field2" => val2}

In your case, I'm assuming your links table has one field named url, so 
you could create a new record in the table by writing:

Link.create("info" => "some url")

An action is called with a url.  The part after the ? in the url gets 
entered into the rail's params hash, which you can access inside the 
action.  For instance, if you enter the following url in your browser's 
address bar:

http://localhost:3000/direct/entry?url=www.someurl.com

that will call the entry method in the DirectController.  Inside the 
entry method, params[:url] will be equal to "www.someurl.com".  How 
would you enter that url in the table?  You can that url in the links 
table by writing the following inside the entry method:

Link.create("info" => params[:url])

or equivalently:

mylink = Link.new
mylink.url = params[:url]
mylink.save




-- 
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to