Reputation: 135
I have a form for a nested resource (Client :has_many Workouts) that I want to pre-populate based on some user input (I use this input to set up some other default parameters for the new action).
<% form_tag new_client_workout_path(@workout_user) do%>
<%= date_select nil, :date %>
<%= submit_tag 'Enter New Workout' %><br/>
<% end %>
If I submit this form I get the following error.
ActionController::MethodNotAllowed Only get, put, and delete requests are allowed.
I can update the form's method to get, which works, but then I have all of my form paramters on the query string which I don't want.
Is there some way to accomplish this using a post?
I've done something similar when the resource wasn't nested and it worked fine.
Upvotes: 0
Views: 1357
Reputation: 25794
I'm not sure if this is directly related to your issue, but when using nested resources, you need to pass both resources to your form tag for it generate the proper routes. Meaning:
<% form_tag new_client_workout_path(@client, @workout) do %>
I don't know enough about the specifics of what you are doing, so it's a little tough to get you to the answer directly. What is @workout_user
? What do your routes look like?
Edit:
After seeing your reply below, the "cleaner way" to do this would be to do:
<% form_tag(new_client_workout_path(@workout_user), :method => :get) do %>
Note: the guides seem to use a quoted "get", but I think a symbol works as well.
Upvotes: 0
Reputation: 7477
So, as I understand it, you want a form (as shown) to be sent as a POST to a controller which causes another form to be rendered which is pre-populated with date-directed parameters (from the first form), right?
It's unconventional but you could just change the new
action of your WorkoutsController
so accept a POST instead of the conventional GET. In routes.rb
map.resources :clients do |clients|
clients.resources :workouts, :new => { :new => :post }
end
UPDATE
The clean RESTful way to do something like this is to have a separate controller (working title - ConfigWorkoutsContoller
) and actions (new
and create
) for the first form as follows;
ConfigWorkoutsContoller#new
renders the form (as shown) which is a GETcreate
action with the date
ConfigWorkoutsContoller#create
pre-populates a @workout instance and renders WorkoutsController#new
Upvotes: 2
Reputation: 135
I was able to get it to work by adding a hidden form field
<%= hidden_field_tag '_method', 'get' %>
so that the form method is post, but the rest method is get
Not sure if I'm doing all this the right way or if there is a more accepted way of getting user input to the new action. Normally, you would get all your user input from the new action, but in this case I'm building the form differently depending on what date the user selects.
Upvotes: 0