rails form_with中的关联模型

时间:2018-02-24 11:17:36

标签: ruby-on-rails forms parameter-passing associations facade

我正在开发一个项目,我必须分析文件中的数据,用户应该可以上传。我有一个模型InputFile并选择为我的Analysis模型实现一个analyze_facade,它捆绑了大部分功能。 这是模型

#app/models/analysis.rb
class Analysis < ApplicationRecord
  has_one :input_file, dependent: :destroy
end

#app/models/input_file.rb
class InputFile < ApplicationRecord
  belongs_to :analysis
  has_many :access_data, dependent: :destroy

  def uploaded_file=(file_field)
    self.name = File.basename(file_field.original_filename).gsub(/[^\w._-]/,'')
    self.content_type = file_field.content_type.chomp
    self.data = file_field.read
  end
end

这是我的AnalysisController和相应的外观模式

#app/controllers/analyses_controller.rb
class AnalysesController < ApplicationController
  before_action :set_analysis, only: :show

  def show
    @analysis_facade = AnalysisFacade.new(@analysis)
  end

  private

  def set_analysis
    @analysis ||= Analysis.create
  end
end

#app/facades/analysis_facade.rb
class AnalysisFacade
  attr_reader :analysis

  def initialize(analysis)
    @analysis = analysis
  end

  def new_input_file
    @input_file = analysis.build_input_file
  end

  def input_file
    @input_file ||= new_input_file
  end

  def access_data
    @access_data ||= analysis.input_file.access_data
  end
end

我的InputFiles控制器

#app/controllers/input_files_controller.rb
class InputFilesController < ApplicationController
  include LogFileScanner

  before_action :set_analysis, only: [:new, :create, :update]

  def new
    @input_file = Input_file.new
  end

  def create
    @input_file = @analysis.build_input_file(input_file_params)
    if @input_file.save
      access_data = scan(@input_file.data)
      @input_file.access_data.create(access_data)
    end
  end

  def update
    @input_file = @analysis.input_file
    if @input_file.update
      access_data = scan(@input_file.data)
      @input_file.access_data.update(access_data)
    end
  end

  private

  def input_file_params
    params.require(:input_file).permit(:uploaded_file, :analysis_id)
  end

  def set_analysis
    @analysis = Analysis.find(params[:input_file][:analysis_id])
  end
end

对于文件上传,我有这些观点

#app/views/analyses/show.html.erb
<%= render template: 'input_files/get' %>

#app/views/input_files/get.html.erb
<%= form_with model: [@analysis_facade.analysis, @analysis_facade.input_file], action: :update, html: {multipart: true, class: 'form-horizontal center'} do |form| %>
  <div class='form-group.new'>
    <% if false %>
      <%= form.hidden_field :analysis_id, value: @analysis_facade.analysis[:id] %>
    <% end %>
    <%= form.file_field 'uploaded_file' %>
    <%= form.submit "Upload", class: 'btn btn-default btn-primary' %>
  </div>
<% end %>

执行此操作会导致错误

undefined method `analysis_input_files_path' for #<#<Class:0x00007f1208040288>:0x00007f11f0d71d48>
Did you mean?  analysis_path

这个地方

<%= form_with model: [@analysis_facade.analysis, @analysis_facade.input_file], action: :update, html: {multipart: true,
class: 'form-horizontal center'} do |form| %>

为什么?我需要将analysis_id传递给params以创建新的InputFile。但我希望它比手动添加hidden_​​field更清洁,因为model_with应该提供此功能。我在这里想念什么?

另外:为什么rails搜索`analysis_input_files_path&#39;?它显然是一个has_one关系,所以它不应该找分析_input_file_path吗?

编辑:

#bin/rails routes

                     Prefix Verb   URI Pattern                                           Controller#Action
                   analyses GET    /analyses(.:format)                                   analyses#index
                            POST   /analyses(.:format)                                   analyses#create
               new_analysis GET    /analyses/new(.:format)                               analyses#new
              edit_analysis GET    /analyses/:id/edit(.:format)                          analyses#edit
                   analysis GET    /analyses/:id(.:format)                               analyses#show
                            PATCH  /analyses/:id(.:format)                               analyses#update
                            PUT    /analyses/:id(.:format)                               analyses#update
                            DELETE /analyses/:id(.:format)                               analyses#destroy
     input_file_access_data GET    /input_files/:input_file_id/access_data(.:format)     access_data#index
                            POST   /input_files/:input_file_id/access_data(.:format)     access_data#create
new_input_file_access_datum GET    /input_files/:input_file_id/access_data/new(.:format) access_data#new
          edit_access_datum GET    /access_data/:id/edit(.:format)                       access_data#edit
               access_datum GET    /access_data/:id(.:format)                            access_data#show
                            PATCH  /access_data/:id(.:format)                            access_data#update
                            PUT    /access_data/:id(.:format)                            access_data#update
                            DELETE /access_data/:id(.:format)                            access_data#destroy
                input_files GET    /input_files(.:format)                                input_files#index
                            POST   /input_files(.:format)                                input_files#create
             new_input_file GET    /input_files/new(.:format)                            input_files#new
            edit_input_file GET    /input_files/:id/edit(.:format)                       input_files#edit
                 input_file GET    /input_files/:id(.:format)                            input_files#show
                            PATCH  /input_files/:id(.:format)                            input_files#update
                            PUT    /input_files/:id(.:format)                            input_files#update
                            DELETE /input_files/:id(.:format)                            input_files#destroy
              analyses_show GET    / 

1 个答案:

答案 0 :(得分:0)

我是一个概念上的错误。我将立面模型改为

org.openqa.selenium.WebDriverException: setWindowRect

视图现在就像这样

profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.download.dir", System.getProperty("user.dir") + "\\downloads");
    profile.setPreference("browser.helperApps.neverAsk.openFile","text/csv,application/x-msexcel,application/excel,application/x-excel,"
            + "application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/x-msexcel,"
            + "application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.focusWhenStarting", false);
    profile.setPreference("browser.download.manager.useWindow", false);
    profile.setPreference("browser.download.manager.showAlertOnComplete", false);
    profile.setPreference("browser.download.manager.closeWhenDone", false);
    profile.setPreference("browser.tabs.remote.autostart", false);
    profile.setPreference("browser.tabs.remote.autostart.1", false);
    profile.setPreference("browser.tabs.remote.autostart.2", false);
    profile.setPreference("browser.tabs.remote.force-enable", "false");