2013-05-23 6 views
0

가져 오기 전에 가져 오기 또는 미리보기를 할 수있는 CSV 파일을 업로드 할 수있는 양식을 만들려고합니다.레일 3.2 또는 4.x 미리보기 용 양식/파일 제출

<%= form_for(@contact_import, :remote => true) do |f| %> 
    <% if @contact_import.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@contact_import.errors.count, "error") %> prohibited this import from completing:</h2> 
     <ul> 
     <% @contact_import.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.file_field :file %> 
    </div> 
    <%= f.submit "Import" %> 
    <%= f.submit "Preview", :name => 'preview' %> 

을 내 컨트롤러 : 내 양식에

내가 가진

def create 
    @contact_import = ContactImport.new(params[:contact_import]) 

    if params[:preview] 
     logger.debug "Let's preview the contacts:" + params.inspect 
     @contacts = @contact_import.update_preview 
     @contact_attributes = ContactImport.mapping_attributes 
     #I should now update the preview div 
    else 
     logger.debug("Got the commit" + params.inspect) 
     if @contact_import.save 
     redirect_to root_url, notice: "Imported contacts successfully." 
     else 
     render :new 
     end 
    end 
    end 

은 어떻게 CSV는 파일을 업로드하여 미리보기 연락처를 표시하는보기를 업데이트 할 수 있습니까?

참고 : 현재 CVS 파일 처리는 모델에 있으므로 생략되었습니다.

답변

2

새 페이지의 다른 버전으로 가져 가서 파일을 구문 분석하고 contact_import 개체를 채우십시오. 숨겨진 변수가 포함 된 페이지를 작성하여 작성 페이지에 제출하십시오.

당신은 단순히이 버튼을 누름에 대해 살펴보고 @contact_import

def create 
    @contact_import = ContactImport.new(params[:contact_import]) 

    if params[:preview] 
     render :preview 
    elsif @contact_import.save 
     redirect_to root_url, notice: "Imported contacts successfully." 
    else 
     render :new 
    end 
    end 

preview.html.erb이 new.html.erb 비슷 파일에서 생성 된 생성하지만 함께 사용하여 미리보기 페이지를 렌더링 할 수 숨겨진 입력 및 뒤로 버튼. 미리보기에서 게시하면 생성되지만 오류 조건이 발생해서는 안됩니다.

새로운 경로가 필요하다고 생각하지 않습니다.이 경우에는 대신에 미리보기 : 미리보기를 사용하십시오.

+0

나는 그가 원격 호출을하고 있다고 생각하는데,이 경우에는 렌더링만으로는 작동하지 않는다. – Noz

+1

죄송합니다 - 게시했을 때 알리지 않았습니다. 파일 필드가있는 원격 양식을 작성하려면 훨씬 더 많은 작업이 필요합니다. https://github.com/JangoSteve/remotipart와 같은 보석을보고 싶을 수도 있습니다. 그러나, 나는 그것이 레일 3.2에서 작동하는지 확신 할 수 없다. – Swards