如何使用一对多关联在纸夹中创建新文件

时间:2014-03-21 15:14:49

标签: ruby-on-rails paperclip

我有2个模型:新闻和上传文件

class News < ActiveRecord::Base
    has_many :uploadedfiles, as: :parent
    attr_accessible :title, :content, :author
end

class Uploadedfile < ActiveRecord::Base
    belongs_to :parent, polymorphic: true
    has_attached_file :url
    attr_accessible :url_file_name, :url_content_type, :url_file_size, :url_updated_at
end

以新闻创作的形式,我补充道:

<div class="field">
<%= f.label :file %><br>
<%= f.file_field :file %>
</div>

和新闻控制器

  def create
    @news = News.new(news_params.except(:file))
        @uploadedfile = @news.uploadedfiles.new(news_params[:file])

    respond_to do |format|
      if @news.save
        format.html { redirect_to @news, notice: 'News was successfully created.' }
        format.json { render action: 'show', status: :created, location: @news }
      else
        format.html { render action: 'new' }
        format.json { render json: @news.errors, status: :unprocessable_entity }
      end
    end
  end

当我将表单发送到sql-table时,会创建一行,但文件列(url_file_name等)为空。 我错了什么?

0 个答案:

没有答案