0

Hi I got a rails form:

<%= form_tag("/orders", action: "find",  method: "get") do %>
        <%= text_field_tag(:barcodeform) %>
        <%= submit_tag("find") %>
    <% end %>

this is my routes.rb

  post 'orders/uploadFile'
   resources :orders do
     member do
       get 'next_state'
     end

     member do
       get 'last_state'
     end

     member do
       get 'last_state_ablauf_2'
     end

     member do
       get 'log'
     end

     member do
       get 'save_Order'
     end

     member do
       get 'auftrag_annehmen'
     end

     member do
       get 'find'
     end
   end

controller: Nothing special i just have set up a method called find in there is redirect to see if it works.

Now when I enter a value into the form it does this:

 http://example/orders?utf8=%E2%9C%93&barcodeform=631&commit=find

But i want it to do something like:

 http://example/orders/531

I don't get any search results, it just shows all entries. How should i do write the form to get this kind of result "/orders/id" ? Any ideas? Thanks

alexMmM
  • 9
  • 1
  • 6
  • Could these be relevant? http://stackoverflow.com/questions/4487796/removing-utf8-from-rails-3-form-submissions?rq=1 (or) http://stackoverflow.com/questions/4104474/rails-3-utf-8-query-string-showing-up-in-url?rq=1 – changingrainbows Nov 05 '13 at 14:55
  • The question is not about the utf8 stuff, It's about the logic behind my code. thanks anyway – alexMmM Nov 05 '13 at 15:05

1 Answers1

0

There are a few things going on, but first you should understand that the utf8 information is generated by the Rails form helper for a reason. If you really don't want it, build your own <form> instead of using the form helper.

Next, since the user is ostensibly supplying your barcodeform, it shouldn't be part of the form's action. You could accomplish something like that with a bit of javascript, but it wouldn't be best practice. You want that barcodeform value as a query parameter so you can access it in your controller with params[:barcodeform] and deal with whatever craziness your user inputs in an appropriate manner.

Lastly you need to fix your routes. Your routes are currently generating a url that looks like this:

http://example/orders/:id/find

Which, since you don't know the :id yet, you do not want. Instead, use collection:

collection do
  get 'find'
end

Now your form should look like:

<%= form_tag("/orders/find", method: "get") do %>
  ...
<% end %>
Community
  • 1
  • 1
hiattp
  • 2,326
  • 1
  • 20
  • 23