0

有時當我提交一個表單然後回到我的瀏覽器時,它會呈現整個表單兩次(見截圖)..不知道是否它的視圖或控制器代碼導致這個,但我似乎無法形象出。Rails Form Renders Twice

感謝

occasions_controller.rb

def create 
    @user = current_user 
    @occasion = Occasion.new(params[:occasion]) 
    @occasion.user = User.find(current_user.id) 
    if @occasion.save 
     redirect_to occasions_path 
    else 
     render :new 
    end 
    end 

user_steps/occasions.html.erb

<%= simple_form_for @user, url: wizard_path do |f| %> 
    <div id="single_module"> 
    <div class="pitch"> 
    <h2 class="signup">Step 3: Your first Occasion!</h2> 
    </div> 
    <div id="input1" style="margin-bottom:4px;" class="clonedInput"> 
    <p>*Just one to get you started, after completion of the sign up process you will have a chance to utilize our occasion wizard to add as many events as you like! (exciting, we know)</p> 
    <ul class="testss1"> 
     <%= f.simple_fields_for :occasions do |occasion_o| %> 
     <li><%= occasion_o.input :name, :placeholder => 'Enter occasion Name', :hint => ('Examples: Anniversaries, Birthdays, Graduations, Mothers day, Fathers day, Valentines day, Weddings, Holidays') %></li> 
     <li><%= occasion_o.input :pname, :label => 'Persons Name', :placeholder => '(optional)' %></li> 
     <li><%= occasion_o.input :dateg, :label => 'Date',:as => :date_picker, :input_html => { :class => 'special' } %></li> 
     <li><%= occasion_o.input :recurring, :as => :radio_buttons, :label => 'Remind me of this event every year?' %></li> 
    <h3>Top 3 Interests</h3> 

     <li class="tes1"><%= occasion_o.association :interests, :label => false, :as => :check_boxes %></li></ul> 
    <%end%> 
    </br> 
    </div> 
     <%= link_to "skip this step", next_wizard_path %> 
     <%= f.button :submit, 'Submit' %> 
    </br> 
    </br> 
    </br> 

enter image description here

回答

0

它看起來像你的用戶有兩個場合把它捆起來。我會檢查與看跌期權聲明場合的長度在你的控制器,檢查你的日誌:

def create 
    @user = current_user 
    puts "The current length of occasions is #{ @user.occasions.length }" 
    @occasion = Occasion.new(params[:occasion]) 
    @occasion.user = User.find(current_user.id) 
    if @occasion.save 
     redirect_to occasions_path 
    else 
     render :new 
    end 
    end 
+0

雅它..我怎麼能阻止它添加一個新的提交一次,然後點擊它的後退按鈕下一頁? – js111

+0

問題出在你的'<%= simple_form_for @user,...'代碼中。當您呈現該視圖時,它將爲您的場合呈現一個表單。如果這種形式的目的只是創建一個新的場合,那麼你應該在控制器中設置'@場合',而不要傳遞'@ user'。我也會拿出'f.simple_fields_for_for:occasion_do | occasion_o |'循環。只要做一個'<%= simple_form_for(@occasion)do | f | %> ...「。參見[documentation](https://github.com/plataformatec/simple_form/) –