0

我有一个表单向导设置,其中包含 4 个步骤,它们都适用于同一模型。

当我填写第一个并单击提交(并被重定向到第 2 步)时,表格已经填写完毕,并在下面添加了一个重复的相同表格。每个步骤都会发生这种情况。

在第 1 步单击提交并进入第 2 步(或 3/4)后,如何呈现一个全新的表单?

步骤1

<%= simple_form_for @user, url: wizard_path do |f| %>

  <h2 class="signup">Step 3: Friends Birthday</h2>


  <%= f.simple_fields_for :events do |event_f| %>
    <%= event_f.input :name, :placeholder => 'Enter Persons Name' %>

    <%= event_f.input :date, :as => :date_picker, :input_html => { :class => 'special' } %>

    <%= event_f.input :reccuring, :label => false,  :inline_label => 'Remind me of this event every year?' %>

    <h3>Choose up to 3 interests for this person</h3>
      <%= event_f.association :interests, :label => false, :as => :check_boxes %>

      <%= event_f.input :kind, :as => :hidden, :input_html => { :value => "Birthday" } %>

<%end%>

    <%= link_to "skip this step", next_wizard_path %>
    <%= f.button :submit, 'Submit' %>
    <%end%>

第2步

<%= simple_form_for @user, url: wizard_path do |f| %>

<h2 class="signup"> Married? Add your anniverarsy</h2>

  <%= f.simple_fields_for :events do |anniversary_f| %>

    <%= anniversary_f.input :name %>
    <%= anniversary_f.input :date, :as => :date_picker, :input_html => { :class => 'special' } %>

    <h3>Choose up to 3 interests for this person</h3>

     <%= anniversary_f.association :interests, :as => :check_boxes, :label => false %></li>

     <%= anniversary_f.input :kind, :as => :hidden, :input_html => { :value => "Anniversary" } %>
     <%end%>


        <%= link_to "skip this step", next_wizard_path %>
        <%= f.button :submit, 'Submit' %>
     <%end%>

控制器

events_controller.rb

  def new
      @user = current_user
      @event = Event.new 
    end

  def create
      @user = current_user
      @event = Event.new(params[:event])
      @event.user = User.find(current_user.id)
   end

users_controller

class UsersController < ApplicationController
  before_filter :authenticate_user!

  def create
      @user = User.new(params[:user])
      if @user.save
        redirect_to user_steps_path
      else
        render :new
      end
    end
4

1 回答 1

1

当您完成一个步骤时,然后插入新对象并将第二种形式绑定到新对象。对第 3 种和第 4 种形式重复此步骤。

意味着每次初始化

     @user = User.new
于 2012-06-21T13:48:14.297 回答