将变量从控制器传递到模型

时间:2015-09-04 20:13:31

标签: ruby-on-rails ruby ruby-on-rails-4

我需要将我的控制器方法中设置的键变量传递给方法键中的模型。

uploadscontroller.rb

def create
  @upload = Upload.new(upload_params)
  @upload.upload_file = params[:file]
  @upload.emission_id = params[:id]
  key = Emission.find(params[:id]).key
end

upload.rb

更新

我需要使用控制器self.path路由中的密钥

class Upload < ActiveRecord::Base
  require 'action_view'
  include ActionView::Helpers::NumberHelper
  belongs_to :emission
  attr_accessor :upload_file
  before_save :upload_file
    def key=(val)
    @variable = val
end

def upload_file=(imcoming_file)

    #en caso de que vengan mas de uno
    imcoming_file.each do |key, value|
        self.name = value.original_filename
        self.tipe = value.content_type
        @data = value.read
        self.path = 'uploads/' + @variable + self.name
        self.md5 = Digest::MD5.hexdigest(@data)
        numer = @data.size
        self.size= number_to_human_size(numer)
        self.time_to_air = "10"

        #Mueve el archivo al directorio
        File.open(Rails.root.join('public', 'uploads', self.name), 'wb') do |file|
            file.write(@data)


        end
    end

1 个答案:

答案 0 :(得分:0)

来自控制器:

@upload.key = key