回形针多态嵌套多文件上传

时间:2013-05-15 21:13:39

标签: ruby-on-rails paperclip nested-attributes polymorphic-associations

没有任何错误消息,只是没有上传文件! 请帮忙!!!!

除了文件上传外,所有内容都已创建完成。

型号:

class Assets < ActiveRecord::Base

  belongs_to :assetable, :polymorphic => true
  delegate :url, :to => :attachment

  attr_accessible :asset, :asset_file_name  

end


class Video < ActiveRecord::Base

attr_accessible :body, :title, :url, :covers, :assets

  has_many :covers, :as => :assetable, :class_name => "Video::Cover", :dependent => :destroy

  accepts_nested_attributes_for :covers, :allow_destroy => true



  class Video::Cover < Assets
   has_attached_file :attachment, :styles => { :small => "300x0>", :large => "800x0>" }
  end





end

视频控制器:

class VideosController < ApplicationController

  #load_and_authorize_resource 

  # GET /videos
  # GET /videos.xml
  def index
    @videos = Video.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @videos }
    end
  end

  # GET /videos/1
  # GET /videos/1.xml
  def show
    @video = Video.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @video }
    end
  end

  # GET videos/new
  # GET /videos/new.xml
  def new
    @video = Video.new

    1.times do @video.covers.build end

    #40.times do @video.images.build end

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @video }
    end
  end

  # GET /collections/1/edit
  def edit
    @video = Video.find(params[:id])
    1.times do @video.covers.build end

    #1.times do @video.images.build end

  end

  # POST /videos
  # POST /videos.xml
  def create
    @video = Video.new(params[:video])

    respond_to do |format|
      if @video.save
        format.html { redirect_to(@video, :notice => 'Video was successfully created.') }
        format.xml  { render :xml => @video, :status => :created, :location => @video }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @video.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /videos/1
  # PUT /videos/1.xml
  def update
    @video = Video.find(params[:id])

    respond_to do |format|
      if @video.update_attributes(params[:video])
        format.html { redirect_to(@video, :notice => 'Video was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @video.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /videos/1
  # DELETE /videos/1.xml
  def destroy
    @video = Video.find(params[:id])
    @video.destroy

    respond_to do |format|
      format.html { redirect_to(videos_url) }
      format.xml  { head :ok }
    end
  end
end

查看:

形式

<div id="form_bk">

<div id="field_left">

<%= form_for @video, :html => {:multipart => true} do |f| %>
  <% if @video.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@video.errors.count, "error") %> prohibited this video from being saved:</h2>

      <ul>
      <% @video.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


    <div id="cover">
        <br />
        <h2>Cover Image</h2>

        <%= f.fields_for :covers do |cover_image| %>

        <% if cover_image.object.new_record? %>
        <p><%= cover_image.file_field :attachment %></p>

        <% end %>
        <% end %>

        <%= f.fields_for :covers do |cover_image| %>

        <% unless cover_image.object.new_record? %>

        <br />
            <p>

            <%=image_tag cover_image.object.url(:small) %><br />
            <br /> Delete cover <%= cover_image.check_box :_destroy %><br /><br />
            </p>

        <% end %>
        <% end %>

    </div>  


    <div id="form_right">

  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
  </div></div>
  <div class="field">
    <%= f.label :url %><br />
    <%= f.text_area :url %>
  </div></div>


    </div>




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

索引视图

<% @videos.each do |video| %>

   <div class="video">

  <%- video.covers.each do |cover| %>       
  <div class="video_cover"><%=link_to image_tag (cover.url(:small)), video %></div>
  <% end %> 


  <div class="video_title"> 
  <%=link_to video.title, video %>

  <%= video.body %>

  <%= link_to 'Show', video %>
  <%= link_to 'Edit', edit_video_path(video) %>
  <%= link_to 'Destroy', video, :confirm => 'Are you sure?', :method => :delete %>  



  </div>


</div>  
<% end %>

<br /><br /><br />
<%= link_to 'New Video', new_video_path %>


<%= debug @videos %>

0 个答案:

没有答案