Rails 4、Paperclip 和多态关联 [英] Rails 4, Paperclip and polymorphic association

查看:56
本文介绍了Rails 4、Paperclip 和多态关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

班级新闻

和形式:

<%= form_for(@news) do |f|%><div class="field"><%= f.fields_for :uploadedfile, f.uploadedfile.new do |uf|%><%= uf.label :url %><br><%= uf.file_field :url %><%结束%>

<div class="actions"><%= f.submit %>

<%结束%>

当我提交表单时,我的表 uploadedfile 没有改变

问题出在哪里?谢谢!

解决方案

我想你已经嵌套了 :uploadedfiles

班级新闻

并在形式:改变:

 <%= f.fields_for :uploadedfile, f.uploadedfile.new do |uf|%>

到:

<%= f.fields_for :uploadedfiles, Uploadedfile.new do |uf|%>

I have 2 models: News and Uploadedfile

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

And form:

<%= form_for(@news) do |f| %>

    <div class="field">

        <%= f.fields_for :uploadedfile, f.uploadedfile.new do |uf| %>
            <%= uf.label :url %><br>
            <%= uf.file_field :url %>
        <% end %>

    </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

When i'm submitting form, my table uploadedfile is not changed

where is the problem? thank you!

解决方案

I think you have nested arribute :uploadedfiles

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

And in form : change:

 <%= f.fields_for :uploadedfile, f.uploadedfile.new do |uf| %>

to:

<%= f.fields_for :uploadedfiles, Uploadedfile.new do |uf| %>

这篇关于Rails 4、Paperclip 和多态关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆