Thursday, May 9, 2013

[Rails] Auto generate models on creation of other models in Ruby on Rails

I have a model called Video that has many statuses. I want to create a status of each kind and add it to @video.statuses in the def create of VideosController.

In VideosController, I have:

 def create      @video = Video.new(params[:video])      Kind.all.each do |f|        @video.statuses.new(:kind =>f, :kind_id=>f.id,:comment =>"", :time_comp => nil, :completed =>false, :video_id =>@video.id)      end      respond_to do |format|        if @video.save          format.html { redirect_to @video, notice: 'Video was successfully created.' }          format.json { render json: @video, status: :created, location: @video }        else          format.html { render action: "new" }          format.json { render json: @video.errors, status: :unprocessable_entity }        end      end    end

However, this returns an error that says that the video could not be saved because the Statuses were invalid. The only validation in my entire project is in status and it merely checks that there is a video_id. I'm very confused why I'm getting this error and would appreciate any help!

https://github.com/ninajlu/videos

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/V3dUCt6Vl-0J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate