如何“创建"在第二步_form之后? [英] How to "Create" after second step _form?
问题描述
因此,在第一步中,用户创建了他的挑战.
<%= form_for(@challenge) do |f| %>
<%= f.text_field :action %>
<%= f.submit %>
<% end %>
然后,他被引导到另一个_form来完成有关该挑战的详细信息.
<%= form_for(@challenge) do |f| %>
<%= f.text_field :action %>
<%= f.date_select :deadline %>
<%= f.check_box :conceal %>
<%= f.submit %>
<% end %>
一旦他添加了这些详细信息并单击保存",我希望通过challenges_controller
的创建"操作来创建挑战.
def step_one
??
end
def create
@challenge = Challenge.new(challenge_params)
@challenge.save
redirect_to challenging_url(@challenge)
end
如果要跨两个请求创建记录,则需要保留第一个请求中的数据,然后将其与第二个请求一起重新提交. /p>
最简单,最"Rails"的方法是从您的第一个请求中接受传入的"name"属性,并以名称保留为隐藏字段的方式呈现第二阶段表单.
app/controllers/challenge_controller
# Show "step 1" form
def new
@challege = Challenge.new
end
# Show "step 2" form, OR, attempt to save the record
def create
@challenge = Challenge.new(params[:challenge])
if params[:step] == '2'
if @challenge.save
redirect_to @challenge, notice: "Challenge saved!"
end
end
# Fall through to render "create.html.erb"
end
app/views/challenges/new.html.erb
<%= form_for @challenge do |f| %>
<%= f.input_field :name %>
<%= f.submit %>
<% end %>
app/views/challenges/create.html.erb
<%= form_for @challenge do |f| %>
<%= f.hidden_field :name %>
<%= hidden_field_tag :step, 2 %>
<%= f.text_field :action %>
<%= f.date_select :deadline %>
<%= f.check_box :conceal %>
<%= f.submit %>
<% end %>
这里有几件事要注意:
-
create
呈现的格式与new
不同.这对于Rails应用程序是非常规的 - create呈现的第2步"表单使用
hidden_field_tag
来在params[:challenge]
属性 的外部提交中附加一个附加值
- 验证未处理-如果有人在步骤1中提交空名称或在步骤2中提交其他无效属性,则显示错误由您决定
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
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
# Show "step 1" form
def new
@challege = Challenge.new
end
# Show "step 2" form, OR, attempt to save the record
def create
@challenge = Challenge.new(params[:challenge])
if params[:step] == '2'
if @challenge.save
redirect_to @challenge, notice: "Challenge saved!"
end
end
# Fall through to render "create.html.erb"
end
app/views/challenges/new.html.erb
<%= form_for @challenge do |f| %>
<%= f.input_field :name %>
<%= f.submit %>
<% end %>
app/views/challenges/create.html.erb
<%= form_for @challenge do |f| %>
<%= f.hidden_field :name %>
<%= hidden_field_tag :step, 2 %>
<%= f.text_field :action %>
<%= f.date_select :deadline %>
<%= f.check_box :conceal %>
<%= f.submit %>
<% end %>
There are a few things to note here:
create
renders a different form thannew
. This is atypical for a Rails application- The "step 2" form rendered by create uses
hidden_field_tag
to attach an extra value to the submission, outside of theparams[:challenge]
attributes - Validation is unhandled - it's up to you to display errors if somebody submits an empty name in step 1, or other invalid attributes in step 2
这篇关于如何“创建"在第二步_form之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!