Rails 3.2.11回形针

时间:2013-05-15 02:36:44

标签: ruby-on-rails ruby-on-rails-3.2 nested-attributes has-many-polymorphs

Rails 3.2.11 Paperclip多态嵌套附件,上传的图像永远不会保存!也许我应该为每个需要图像的表添加资产ID?

什么会阻止回形针上传和保存文件?

先谢谢你

错误消息:

Connecting to database specified by database.yml
Started GET "/videos/" for 31.54.205.49 at Wed May 15 06:51:33 -0500 2013
Processing by VideosController#index as HTML
  Rendered videos/index.html.erb within layouts/application (111.6ms)
Completed 500 Internal Server Error in 227ms

ActionView::Template::Error (undefined method `attachment_file_name' for #<Video::Cover:0x6929d69365c0>):
    3:    <div class="video">
    4: 
    5:   <%- video.covers.each do |cover| %>        
    6:   <div class="video_cover"><%=link_to image_tag (cover.url(:small)), video %></div>
    7:   <% end %> 
    8: 
    9:   
  app/models/assets.rb:4:in `__send__'
  app/models/assets.rb:4:in `url'
  app/views/videos/index.html.erb:6:in `_app_views_videos_index_html_erb__730847823_57814209936840'
  app/views/videos/index.html.erb:5:in `_app_views_videos_index_html_erb__730847823_57814209936840'
  app/views/videos/index.html.erb:1:in `each'
  app/views/videos/index.html.erb:1:in `_app_views_videos_index_html_erb__730847823_57814209936840'
  app/controllers/videos_controller.rb:10:in `index'

架构:

ActiveRecord::Schema.define(:version => 20130514180732) do

  create_table "assets", :force => true do |t|
    t.datetime "created_at",               :null => false
    t.datetime "updated_at",               :null => false
    t.string   "type"
    t.integer  "assetable_id"
    t.string   "assetable_type"
    t.string   "attachement_type"
    t.string   "attachement_file_name"
    t.string   "attachement_content_type"
    t.integer  "attachement_file_size"
    t.datetime "attachement_updated_at"
  end

  create_table "photos", :force => true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "videos", :force => true do |t|
    t.string   "title"
    t.text     "body"
    t.string   "url"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

end

模型:

class Assets < ActiveRecord::Base

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


end





class Video < ActiveRecord::Base

attr_accessible :body, :title, :url, :assets_attributes, :cover

  #has_many :images, :as => :assetable, :class_name => "Video::Image", :dependent => :destroy
  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 collection 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| %>

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

        <% end %>
        <% end %>

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

        <% unless cover.object.new_record? %>

        <br />
            <p>

            <%=image_tag cover.object.url(:small) %><br />
            <br /> Delete cover <%= cover.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 %>

0 个答案:

没有答案