2015-01-03 1 views
-1

저는 루비를 처음 사용하는데 도움이되었습니다. 루비에서 파일 업 로더를 만들었습니다. 별도로 테스트 할 때 완벽하게 작동합니다. 그러나 다른 형식으로 렌더링하면 작동하지 않습니다. 이벤트/new.html.erb다른 형식으로 렌더링 할 때 Ruby에서 파일 업로드가 작동하지 않습니다.

업로드/_index.html.erb

<h4>Upload File </h4> 
<%= form_tag({:controller => 'upload', :action => 'upload_image'}, :multipart => true, remote: true) do %> 
    <p><label for="upload_file">Select file..</label> 
     <%= file_field 'upload', 'datafile' %></p> 
    <%= submit_tag "Upload", :class => "btn btn-default btn-lg active", :method => 'post' %> 
<% end %> 

data_file.rb 내가 렌더링 한

class DataFile < ActiveRecord::Base 
    attr_accessor :upload 
    belongs_to :event 

    def self.save_file(upload) 

    file_name = upload['datafile'].original_filename if (upload['datafile'] !='') 
    file = upload['datafile'].read 

    file_type = file_name.split('.').last 
    new_name_file = Time.now.to_i 
    name_folder = new_name_file 
    new_file_name_with_type = "#{new_name_file}." + file_type 

    directory = 'public/data' 


    Dir.mkdir(directory + "#{name_folder}"); 
    File.open(directory + "#{name_folder}/" + new_file_name_with_type, "wb") do |f| 
     f.write(file) 
    end 

    end 


end 


upload_controller.rb 

class UploadController < ApplicationController 
    before_filter :authenticate_user! 
    before_filter do 
    redirect_to :root unless current_user && current_user.isAdmin? 
    end 


    def index 
    # @users = User.find(params[:id]) 
    redirect_to new_event_path 
    end 

    def upload_image 
    DataFile.save_file(params[:upload]) 
    #redirect_to new_event_path 
    end 

    end 

업로드/인덱스 : 여기 내 코드입니다

<div style="margin-left: 360px;"><h1>New event</h1></div> 
<br/> 
<br/> 

<div style="margin-left: 360px;"> 

    <%= form_for(:event, :url => {:controller => 'events', :action => 'new'}, html: { method: :post }) do |f| %> 

     <% if @event.errors.any? %> 
      <div id="error_explanation"> 
      <h2><%= pluralize(@event.errors.count, "error") %> prohibited this event from being saved:</h2> 

      <ul> 
       <% @event.errors.full_messages.each do |message| %> 
        <li><%= message %></li> 
       <% end %> 
      </ul> 
      </div> 
     <% end %> 

     <div class="field"> 
     <%= f.label :event_Day %><br> 
     <select name="event_day_id"> 
      <option value="0">-Choose agenda item-</option> 
      <% for agenda_item in @agenda_items %> 
       <option value="<%= agenda_item.id %>"><%= agenda_item.name %></option> 
      <% end %> 
     </select> 
     </div> 

     <%= link_to 'Add new agenda item', new_agenda_item_path %> 
<br/> 
     <br/> 
     <div class="field"> 
     **<%= render :partial => 'upload/index' %>** 
     </div> 

     <br/> 

     <div class="field"> 
     <div class="field"> 
      <%= f.label :Survey %><br> 
      <select name="survey_id"> 
      <option value="0">-Choose survey-</option> 
      <% for survey in @surveys %> 
       <option value="<%= survey.id %>"><%= survey.title %></option> 
      <% end %> 
      </select> 
     </div> 
     <%= link_to 'Create new survey', new_survey_path %> 
     </div> 

     <div class="actions"> 
     <%= f.submit :class => "btn btn-default btn-lg active", :method => 'post' %> 
     </div> 
    <% end %> 

</div> 

<%= button_to 'Back', events_path, :class => 'btn btn-primary btn-lg active', :method => 'get' %> 

감사!

+0

리 팩터를 읽고 대답하기가 어렵습니다. – ianaya89

답변

0

죄송합니다. 질문이 불완전하다는 것을 알게되었습니다. 나는 그 질문에 대답했다. 업로드 파일 양식을 다른 양식으로 렌더링 할 때 작동하지 않습니다. 그래서 나는 그것을 폼에서 제거하고 맨 위에 놓았고 모든 것이 잘 작동합니다.

한 번 다른 양식 안에 서식을 중첩하는 방법이 없다면?

감사합니다.

관련 문제