Rails控制器中的嵌套属性检索

时间:2012-04-25 11:25:07

标签: ruby-on-rails

这是我的代码结构。我有一个视频附上每个响应,据我所知,我已成功上传它。当我需要在保存结构后转换它时,问题就出现了。我希望访问新更新的嵌套属性(请参阅lesson_controller),但我不确定如何执行此操作。

非常感谢!

码头。

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 个答案:

答案 0 :(得分: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