Zabba
Zabba

Reputation: 65467

How to redirect from one form to another and passing a value to the second form?

I have a "mini" form on the home page which allows the user to select a car color and press submit. After doing so, the user is taken to another form where the previously selected car color is used to pre-populate a field in this other form.

The mini form is this:

<%= form_tag('/cars/new', :method => :get) %>
  <%= select "new_car", "color_id", Colors.find(:all, :order => "description asc").
        collect {|s| [ s.description, s.id ] }, {:include_blank => 'Select color'} %>

  <input type="submit" value="Submit"/>

On pressing submit, this routes to the correct action (/cars/new) but the URL in the address bar is:

http://localhost:3000/cars/new?utf8=✓&new_car[color_id]=12

Where I expected it to be:

http://localhost:3000/cars/new?color_id=12

How to make get the URL to look like the one above?

Another question, how exactly is the form_tag to be used? How do you put a closing </form> tag?

Upvotes: 1

Views: 503

Answers (1)

TK-421
TK-421

Reputation: 10763

For the block form of form_tag, see:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-form_tag

For the query string issue, there's a similar question here:

removing "utf8=✓" from rails 3 form submissions

Upvotes: 1

Related Questions