Rails 3 Create method using nested resources?

Posted by user1461119 on Stack Overflow See other posts from Stack Overflow or by user1461119
Published on 2012-06-16T21:10:44Z Indexed on 2012/06/16 21:16 UTC
Read the original article Hit count: 192

How can I clean this up using rails 3 features? I have a post that belongs to a group and also a user. The group and user has_many posts. I am using a nested resource

resources :groups do
    resources :posts
end



<%= form_for @post, :url => group_posts_path(params[:group_id]) do |f| %>
....
<% end %>

 def create
   @group = Group.find(1)
   @post = @group.posts.build(params[:post])
   @post.user_id = current_user.id

   respond_to do |format|
      if @post.save
      .....
      end
   end
 end

Thank you.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails-3