Jump to content

The ultimate community for Ruby on Rails developers.


Photo

“Create” Action for nested resource with multiple Associations

problem help activerecords associations create resource nested resource action controller

  • Please log in to reply
1 reply to this topic

#1 S0lll0s

S0lll0s

    Passenger

  • Members
  • 3 posts

Posted 11 May 2014 - 06:26 PM

Hello everyone. I am a rails newbie trying to set up his first real project, and so far I think I got along rather well.
This is the first problem I can't seem to solve alone...
 
About my setup:
I have items, that each have multiple (threaded) comments.
The threading is done via a polymorphic parent key.
 
I just can't get the create action to work properly, I got it far enough to submit into the database, but it didn't have item_id and parent_id set.
 
I tried form_for [@item, @comment] instead of form_for :comment, url: item_comments_path( @item ) but it didn't help either.
 
When I look at comment_params in the create action I get this:
 
Parameters: {"utf8"=>"✓", "comment"=>{"body"=>"examplecommentgoeshere"}, "parent_id"=>"4", "commit"=>"Create Comment", "item_id"=>"4"}
Can anyone help?
 
Here are the relevant pieces of code:
 
Models:
    class Item < ActiveRecord::Base
      belongs_to :user
      has_many :comments, dependent: :destroy
      has_many :images,   as: :item,
                          dependent: :destroy
    
      validates :title,       presence: true,
                              allow_blank: false
      validates :description, presence: true,
                              allow_blank: false
      validates :user,        presence: true,
                              allow_blank: false
      validates :layout,        presence: true
    end




    class Comment < ActiveRecord::Base
      belongs_to :user
      belongs_to :item
      has_many :responses,  class_name: "Comment",
                            foreign_key: "parent_id",
                            dependent: :destroy
      has_one :image,       as: :item,
                            dependent: :destroy
      belongs_to :parent,   class_name: "Comment"
    
      validates :body,      presence: true,
                            allow_blank: false,
                            length: { minimum: 10 }
    end
 
Comment Controller:
    class CommentsController < ApplicationController
      before_filter :findParent
      before_filter :find, only: [:update, :destroy, :show]
      before_filter :is_logged_in?, only: [:create, :update, :destroy]
      before_filter :has_permission?, only: [:update, :destroy]
    
      def new
        @comment = Comment.new
      end
    
      def create
        logger.debug comment_params.inspect
        @comment = current_user.comments.build(comment_params)
    
        if @comment.save
          redirect_to @item
        else
          logger.debug @comment.errors.inspect
          session[:errorbj] = @comment
          redirect_to @item
        end
      end
      [...]
      private
      def comment_params
        params.require(:comment).permit(:body, :parent)
      end
    
      private
      def find
        @comment = Comment.find(params[:id])
      end
      
      private
      def findParent
        @item = Item.find params[:item_id]
      end
Item View:
    <p>
    <h3>Add a comment:</h3>
      <%= render partial: 'comments/form', locals: { parent: @item } %>
    </p>
 
Partial comments/form:
    <%= form_for :comment, url: item_comments_path( @item ) do |f| %>
      <p>
        <%= f.label :body %><br>
        <%= f.text_area :body %>
      </p>
    
      <p>
        <%= f.label :image %><br>
        &lt;nope&gt;
      </p>
    
      <%= hidden_field_tag :parent_id, parent.id %>
      <p>
        <%= f.submit %>
      </p>
    <% end %>

I asked this question on stackoverflow yesterday, but I haven't got a response yet.



#2 S0lll0s

S0lll0s

    Passenger

  • Members
  • 3 posts

Posted 13 May 2014 - 05:45 PM

bump...







Also tagged with one or more of these keywords: problem, help, activerecords, associations, create, resource, nested resource, action, controller

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users