0

I have this in my view; I made the "new" and "edit" views similar with only the following snippet which dictates if the form is a patch or a post.

<% @selected_holiday[:id].present? ? (current_method = :patch) : (current_method = :post) %>
<%=
    form_tag( {method: current_method},
              class: 'create_form',
              id: "new_holiday_form",
              :"data-parsley-validate" => "" ) do
%>

My routes contains:

                  human_resources_settings_holidays GET      /human_resources/settings/holidays(.:format)                         human_resources/settings/holidays#index
                                                      POST     /human_resources/settings/holidays(.:format)                         human_resources/settings/holidays#create
                 new_human_resources_settings_holiday GET      /human_resources/settings/holidays/new(.:format)                     human_resources/settings/holidays#new
                edit_human_resources_settings_holiday GET      /human_resources/settings/holidays/:id/edit(.:format)                human_resources/settings/holidays#edit
                     human_resources_settings_holiday GET      /human_resources/settings/holidays/:id(.:format)                     human_resources/settings/holidays#show
                                                      PATCH    /human_resources/settings/holidays/:id(.:format)                     human_resources/settings/holidays#update
                                                      PUT      /human_resources/settings/holidays/:id(.:format)                     human_resources/settings/holidays#update

Yet from

http://localhost:3000/human_resources/settings/holidays/7d525fe6-b1e7-11e5-b6e8-00ff6a8ddd39/edit

I get:

http://localhost:3000/human_resources/settings/holidays/7d525fe6-b1e7-11e5-b6e8-00ff6a8ddd39/edit?method=patch

and therefore:

The action '7d525fe6-b1e7-11e5-b6e8-00ff6a8ddd39' could not be found for HumanResources::Settings::HolidaysController

How do I not hit the edit path and go through the PATCH?

james
  • 515
  • 4
  • 14

2 Answers2

0

Try

<% @selected_holiday[:id].present? ? (current_method = :patch) : (current_method = :post) %>
<%=
    form_tag(human_resources_settings_holiday_path, method: current_method,
              class: 'create_form',
              id: "new_holiday_form",
              :"data-parsley-validate" => "" ) do
%>

The first argument in your example make is a part of the URL params. You can also pass nil as the first argument if you want to specify current page as action

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

You probably want to switch the form action path accordingly as well

Rajesh Sharma
  • 419
  • 2
  • 5
0

This might be a good time to refactor so that you don't have to do this checking at all. The rails form_for :resource helper allows you have pass in the resource, in this case @selected_holiday, and it will create the appropriate methods/actions.

Rails form_for documentation

AdamCooper86
  • 3,957
  • 1
  • 13
  • 19