1

How do I pass parameters to a Rails application using a normal web url?

For instance if I have..

http://0.0.0.0:3000/lists/create/list[name]=Paul&list[age]=39&list[tag]=misc

So I have created a controller called lists and I want to pass a name, age and a tag..

In my example I am passing..

name = Paul age = 39 tag = misc

My example I pasted above says that the item was created but the item it adds has empty data, suggesting my formatting isn't correct.

Could anyone tell me how I structure the url above to pass the parameters correctly?

Thanks Paul

2
  • how the heck are you posting to 0.0.0.0 ? Commented Nov 5, 2010 at 22:29
  • hmm thats the default address I get told to connect to when I run script/server, I also tried localhost:3000 but still get the same problems, this is all been tested locally at the moment Commented Nov 5, 2010 at 22:30

1 Answer 1

3

By default, Rails's RESTful routing prohibits the create action via the GET (regular url) protocol. For your specific example, you would need to add this route to your config/routes.rb file:

map.create_list 'list/create', :controller => 'lists', :action => 'create', :conditions => { :method => :get }

This adds a route create_list_path or create_list_url that is accessible via GET for links, etc. The url used to create a list directly would be:

http://0.0.0.0:3000/lists/create?list[name]=Paul&list[age]=39&list[tag]=misc

Also note that if you are getting errors about invalid authenticity tokens, you may need to add this line to your controller:

skip_before_filter :verify_authenticity_token, :only => :create

For more general cases, you configure routes similarly and forms as follows:

You need to specify :method => 'get' in your form_tag.

This is discussed in the Ruby on Rails Form Helpers guide (search for "A Generic Search Form").

The basic code given that should get you started is

<%= form_tag(search_path, :method => "get") do %>
  <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
<% end %>

generates

<form action="/search" method="get">
  <label for="q">Search for:</label>
  <input id="q" name="q" type="text" />
  <input name="commit" type="submit" value="Search" />
</form>

which GETs the url: http://my.server/search?q={query input}&commit=Search.

Sign up to request clarification or add additional context in comments.

9 Comments

I don't really have my own form at this stage, other than what the scaffold would have generated
In the scaffold-generated form you should be able to add :method => 'get' to put the parameters in the url.
Is there anyway of doing this without forms, I ultimately want to be able to just call the url passing a few parameters so it wont use forms at all
Are you sure that you are using the correct url for your GET request? http://0.0.0.0:3000/lists/create/list[name]=Paul&list[age]=39&list[tag]=misc should be http://0.0.0.0:3000/lists/create?list[name]=Paul&list[age]=39&list[tag]=misc.
This is part of the problem and what i'm trying to figure out with all this, I don't know what the correct url should be. The url I posted creates an empty entry in the db so i'm guessing its wrong somewhere. How do I find out what the correct url should be?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.