So in step one the user creates his challenge.
<%= form_for(@challenge) do |f| %>
<%= f.text_field :action %>
<%= f.submit %>
<% end %>
Then he is directed to another _form to finish adding details about that challenge.
<%= form_for(@challenge) do |f| %>
<%= f.text_field :action %>
<%= f.date_select :deadline %>
<%= f.check_box :conceal %>
<%= f.submit %>
<% end %>
Once he adds those details and clicks "Save" I want the challenge to be created via the Create action of the challenges_controller
.
def step_one
??
end
def create
@challenge = Challenge.new(challenge_params)
@challenge.save
redirect_to challenging_url(@challenge)
end
The answer is more likely "it depends what you are trying to do", but the simplest solution is to redirect (in create after save) to the edit action/view which contains all or the other fields, not just the limited fields you provided in the new action/view.
If you want to create a record across two requests, you need to persist data from the first request, and re-submit it along with the second request.
The easiest and most "Rails"y way of accomplishing this is to accept the incoming "name" attribute from your first request and render the second stage form with the name persisted as a hidden field.
app/controllers/challenge_controller
app/views/challenges/new.html.erb
app/views/challenges/create.html.erb
There are a few things to note here:
create
renders a different form thannew
. This is atypical for a Rails applicationhidden_field_tag
to attach an extra value to the submission, outside of theparams[:challenge]
attributes