tibbon
tibbon

Reputation: 1028

Using accepts_nested_attributes_for, has_many and creating just one entry

I have a Page with many Comments. Many users can access this page and submit comments. I view the comments on another Page that is private.

models/page.rb

class Page < ActiveRecord::Base
  has_many :comments, :dependent => :destroy 
  accepts_nested_attributes_for :comments, :allow_destroy => true
end

models/comment.rb

class Comment < ActiveRecord::Base
  belongs_to :page
  belongs_to :user
end

views/pages/show.html.erb relevant section

<%= form_for @page, :remote => true do |f| %>
    <% f.fields_for :comments do |builder| %>
   <%= builder.text_area :content, :rows => 1 %>
   <%= builder.submit "Submit" %>
    <% end %>
<% end %>

controllers/pages_controller.rb

def show
  @page = Page.find(params[:id])
end

def update
  @page = Page.find(params[:id])
  @page.comments.build
  @page.update_attributes(params[:page])
end

The issue here is that I do not want to have the user see multiple fields for comments. Yet, if I do <% f.fields_for :comment do |builder| %> then it throws an error because it doesn't know what one comment is, and only wants to deal with multiple.

Essentially, the user should be able to submit a single comment on a page, which has many comments, and have that automatically associated with the page. As a secondary thing, I need to have the user's id (accessible through current_user.id via Devise) associated with the comment.

Upvotes: 4

Views: 1951

Answers (2)

Alex Bartlow
Alex Bartlow

Reputation: 106

<% f.fields_for :comments, f.object.comments.build do |fc| %>
  <!-- rest of form -->
<% end %>

Upvotes: 9

Gary Pinkham
Gary Pinkham

Reputation: 43

could you use nested resources? basically in the routes.rb...

resources :pages do 
  resources :comments 
end     

then in the comments controller you can find the pages by page_id or something like that.. Don't recall exact syntax off top of head..

Upvotes: 0

Related Questions