2017-02-01 8 views
1

我有一個Rails應用程序,我在彈出窗口中有窗體。我希望在用戶點擊提交後關閉窗口。如果我使用渲染布局,則無法關閉彈出窗口

在我的application.js,我有

function windowClose() { 
    "setTimeout(windowClose(), 15000);"; 
} 

我的形式

<%= form_tag(controller: 'bedsheet_lines', action: 'bedsheet_line_comments', method: "get", id: '3081') do %> 

    <%=text_area_tag e_comment, @current_bedsheet_line.comments %></td> 

    <%= submit_tag "submit", :onclick => "setTimeout(windowClose(), 5000);"%> 

<% end %> 

我的路線是

get '/bedsheet_liner/comments' =>'bedsheet_lines#bedsheet_line_comments', as: 'bedsheet_line_comments' 
    post '/bedsheet_liner/comments' => 'bedsheet_lines#bedsheet_line_comments' 

我控制器

def bedsheet_line_comments 

    @bedsheet_line = BedsheetLine.find(3081) 

    render :layout => 'alternate' 

    if params[:comments].present? 
     @bedsheet_line = @bedsheet_line.update_attributes(:comments => params[:comments]) 
    end 

    # redirect_to bedsheet_line_path(@bedsheet_line.id) 

    end 

我需要撥打render :layout => 'alternate',以便彈出窗口不包含application.html.erb提供的頁眉,頁腳或導航欄。

如果我嘗試調用被註釋掉的redirect_to bedsheet_line_path(@bedsheet_line.id),我得到

AbstractController::DoubleRenderError in BedsheetLinesController#bedsheet_line_comments 
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return". 

我試圖努力解決此通過使用Javascript(<%= submit_tag "submit", :onclick => "setTimeout(windowClose(), 5000);"%>)後提交關閉窗口,但而關閉窗口,它防止從寫入更新。這個延遲是試圖讓Rails在關閉窗口之前完成寫入數據的嘗試。

總結 - 我希望能夠呈現替代佈局,並在用戶點擊提交按鈕後重定向用戶。

回答

1

首先,你應該分開你的get和post操作。

讓我們說的get請求被show_bedsheet_line_comments行動服務:

def show_bedsheet_line_comments 
    @bedsheet_line = BedsheetLine.find(3081) # I don't know if this is used in your view 
    render :layout => 'alternate' 
end 

和您的POST請求被bedsheet_line_comments行動服務:

def bedsheet_line_comments 
    @bedsheet_line = BedsheetLine.find(3081) 
    if params[:comments].present? 
    @bedsheet_line = @bedsheet_line.update_attributes(:comments => params[:comments]) 
    end 
    render js: "setTimeout(windowClose(), 5000);" 
end