Recover Nested Rails Attributes in a Controller

Here is the structure of my code. I have a video attached with each answer, and as far as I can tell, I managed to upload it. The problem arises when I need to transform it after saving the structure. I want to access a recently updated nested attribute (see Lesson_controller), but I'm not sure how to do this.

Many thanks!

Pierre.

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

and here 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"

Here is my html view

<% @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>

and here lesson_controller.rb - update

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
+3
source share
1 answer

I think I should answer my question.

Mostly for 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
+1
source

All Articles