0

I am trying to create a form that redirects to a method called accept_item on the form controller. My issue is that it's not going through the accept_item method when I submit the form.

form_controller.rb

def accept
    @form = Form.find(params['id'])
end
def accept_item
    redirect_to inventories_path
end

accept.html.erb

  <%= form_tag accept_item_forms_url do %>
      <% @form.items.each do |i| %>
          <%= label_tag 'Item Name' %>
          <p><%= i.name %></p>
          <%= label_tag 'Quantity' %>
          <p><%= text_field_tag 'quantity', i.quantity %></p>

      <% end %>
      <%= submit_tag 'Accept', class: 'btn btn-default btn-about pull-left', data: {confirm: 'Are you sure you want to accept?'} %>
  <% end %>

routes.rb

  resources :forms do
    collection do
      get :accept_item, :as => :accept_item
    end
  end

Error Message

No route matches [POST] "/forms/accept_item"
myhouse
  • 1,911
  • 1
  • 17
  • 24

2 Answers2

1

The form_tag helper uses the HTTP POST method by default. You defined your routes with get:

get :accept_item, :as => :accept_item

You should use post instead:

post :accept_item, :as => :accept_item

Also I don't think you need the as: :accept_item part, unless you're going to use accept_item_url instead of accept_item_forms_url.

Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
  • I have multiple items in my input field. How can I send it to my controller as an array? – myhouse Sep 11 '16 at 01:18
  • @MarcosIcaza checkout [this](http://stackoverflow.com/questions/6992112/text-fields-from-array-in-rails) and [this](http://stackoverflow.com/questions/3089849/ruby-on-rails-submitting-an-array-in-a-form) – Tamer Shlash Sep 11 '16 at 01:24
0

just remove :as => :accept_item, and change method to post

post :accept_item

You will get /foo when using :as => 'foo'.

Arun Kumar Mohan
  • 11,517
  • 3
  • 23
  • 44
Dapeng114
  • 207
  • 2
  • 10