我已经为此工作了几天,到处搜索让我以某种方式接近我的目标。
我正在尝试在我的应用程序中上传多个图像。
电影.rb
class Movie < ActiveRecord::Base
attr_accessible :title, :notes, :link, :screenshots_attributes
has_many :comments, :as => :commentable
has_many :screenshots
accepts_nested_attributes_for :screenshots, allow_destroy: true
belongs_to :cast
end
截图.rb
class Screenshot < ActiveRecord::Base
attr_accessible :image, :movie_id
belongs_to :movie
mount_uploader :image, ImageUploader
end
电影控制器.rb
def create
@movie = Movie.new(params[:movie])
@cast = Cast.find(params[:cast_id])
if @release.save
redirect_to cast_movie_path(params[:cast_id], @movie), notice: "#{t 'movie.created'}"
else
render action: "new"
end
end
_form.html.rb(电影)
<%= form_for [@cast, @movie], :id => 'fileupload' do |f| %>
<%= f.text_field :movie %></div><%= t 'movie.title' %><br />
<%= f.fields_for :screenshots do |s| %>
<%= s.file_field :image, multiple: true %><%= t 'movie.screenshots' %><br />
<% end %>
<%= f.text_area :notes %></div><%= t 'movie.notes' %></div><br />
<div class="actions">
<%= f.submit "#{ t 'general.submit'}", :class => "btn"%>
</div>
<% end %>
当我提交表单时,只有一个图像被上传,尽管当我“提高 params.to_yaml”(用于调试目的)时,我得到 NoMethodError
undefined method `name' for nil:NilClass