Rails/ActiveRecord has_many question?

If so, I believe the real routing structure you want is: resources :videos do resources :comments end This will make a path like video_comments_path(@video) which you can POST to and in the create action do something like: video = Video. Find(params:video_id) @comment = @video.comments. Create(params:comment) Thats basically it.

You're not specifying params:video_id correctly. From what I can tell you want to allow users to comment on videos? If so, I believe the real routing structure you want is: resources :videos do resources :comments end This will make a path like video_comments_path(@video) which you can POST to and in the create action do something like: @video = Video.

Find(params:video_id) @comment = @video.comments. Create(params:comment) Thats basically it.

Thanks, this helped me understand params better. I followed this from the peepcode, I am still learning Rails. However when doing it this way it leaves me with a routing error.

– ericraio Sep 25 at 0:23.

You could use a nested resource (I think they are there in 2.3.5, not sure). Or else you could modify your partial to pass the video_id parameter: _form.html. Erb comments_path(@video) do |f| %> 'Submiting...' %.

That worked! I believe that I tried doing hidden_field_tag but I wrote it out as 'f. Dden_field_tag' – ericraio Sep 25 at 0:06 Could also actually set the video_id on the model in the form, and avoid having to do it in the controller, via simply @video.Id %> – numbers1311407 Sep 25 at 0:06 @numbers1311407 I think that using f.

Dden_field will give you a name like commentvideo_id rather than video_id. – fd. Sep 25 at 10:20.

So I am trying to figure out why my has_many association doesn't work properly. I have 2 tables in the database, Videos and Comments. Inside the Comments table I have a foreign key of video_id column and when posting a comment nothing shows up on my video's page because the video_id column is NULL so nothing got posted.. If I add the id of the video in the video_id column, you can view the comment.

Am I missing something?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions