这是我的代码的结构。我在每个 cresponse 中都附有一个视频,据我所知,我已经成功上传了它。当我需要在保存结构后对其进行转换时,问题就来了。我希望访问新更新的嵌套属性(请参阅课程控制器),但不知道如何去做。
非常感谢!
码头。
课程.rb
class Lesson < ActiveRecord::Base
has_one :user
has_many :comments, :dependent => :destroy
has_many :cresponses, :dependent => :destroy
acts_as_commentable
accepts_nested_attributes_for :comments, :reject_if => lambda { |a| a[:body].blank? }, :allow_destroy => true
accepts_nested_attributes_for :cresponses
这是 cresponse.rb
class Cresponse < ActiveRecord::Base
belongs_to :lesson
attr_accessible :media, :accepted, :description, :user_id
# NOTE: Comments belong to a user
belongs_to :user, :polymorphic => true
# Paperclip
require 'paperclip'
has_attached_file :media,
:url => "/system/:lesson_id/:class/:basename.:extension",
:path => ":rails_root/public/system/:lesson_id/:class/:basename.:extension"
这是我的 HTML 视图
<% @cresponse = @lesson.cresponses.build %>
<%= form_for @lesson, :html => { :multipart => true } do |f| %>
<td class="list_discussion" colspan="2">
<div class="field">
<%= f.fields_for :cresponses, @cresponse, :url => @cresponse, :html => { :multipart => true } do |builder| %>
Upload : <%= builder.file_field :media %><br />
Description : <%= builder.text_field :description %>
<%= builder.hidden_field :user_id , :value => current_user.id %>
<% end %>
</div>
</td>
这是lesson_controller.rb - 更新
def update
@lesson = Lesson.find(params[:id])
respond_to do |format|
if @lesson.update_attributes(params[:lesson])
**if @lesson.cresponses.** <-- not sure how to find the cresponse that I need to convert
puts("Gotta convert this")
end