2012-04-25 56 views
3

下面是我的代碼的結構。我有一個視頻附有每個響應,並據我所知我已成功上傳它。當結構保存後需要將其轉換時,問題就出現了。我希望訪問新更新的嵌套屬性(請參閱lesson_controller),但不知道如何去做。Rails在控制器中嵌套屬性檢索

非常感謝!

碼頭。

lesson.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 

回答

1

認爲我應該回答我的問題..

基本上爲lesson_controller.rb

params[:lesson][:cresponses_attributes].values.each do |cr| 
@cresponse_user_id = cr[:user_id] 
@cresponse_description = cr[:description] 
if cr[:media] 
    .... and so on