ruby on rails - How to Submit Polymorphic Comments on Feed? [Error] -


if user clicks [+ comment] button

enter image description here

he confronted evil beast:

activerecord::recordnotfound in commentscontroller#create couldn't find comment 'id'= line: @commentable = resource.singularize.classify.constantize.find(id) 

activities/index

<%= link_to activity.user.name, activity.user %> <%= render "activities/#{activity.trackable_type.underscore}/#{activity.action}", activity: activity %> <%= render "comments/comments", comments: activity.comments %> <%= render "comments/form", new_comment: comment.new(commentable_id: activity.id, commentable_type: activity.class.model_name) %> 

comments/_form

<%= form_for new_comment |f| %>   <%= f.text_area :content, rows: 4, class: 'form-control', placeholder: 'enter comment' %>   <%= button_tag(type: 'submit', class: "btn") %>     <span class="glyphicon glyphicon-plus"></span> comment   <% end %> <% end %> 

activities_controller

def index   @activities = activity.order("created_at desc").where(user_id: current_user.following_ids)   @commentable = @activity   @comment = comment.new end 

the error can found here:

class commentscontroller < applicationcontroller   before_action :load_commentable   before_action :set_comment, only: [:show, :edit, :update, :destroy, :like]   before_action :logged_in_user, only: [:create, :destroy]      def index         @comments = @commentable.comments     end      def new         @comment = @commentable.comments.new     end      def create         @comment = @commentable.comments.new(comment_params)         if @comment.save             redirect_to @commentable, notice: "comment created."         else             render :new         end     end      def edit         @comment = current_user.comments.find(params[:id])     end      def update         @comment = current_user.comments.find(params[:id])         if @comment.update_attributes(comment_params)             redirect_to @commentable, notice: "comment updated."         else             render :edit         end     end      def destroy         @comment = current_user.comments.find(params[:id])         @comment.destroy         redirect_to @commentable, notice: "comment destroyed."     end    def     @comment = comment.find(params[:id])     @comment_like = current_user.comment_likes.build(comment: @comment)     if @comment_like.save             @comment.increment!(:likes)         flash[:success] = 'thanks liking!'     else         flash[:error] = 'two many likes'       end           redirect_to(:back)   end  private   def set_comment     @comment = comment.find(params[:id])   end      def load_commentable         resource, id = request.path.split('/')[1, 2]         @commentable = resource.singularize.classify.constantize.find(id) #here is!     end      def comment_params         params[:comment][:user_id] = current_user.id         params.require(:comment).permit(:content, :commentable, :user_id, :like)     end end 

the error came answer here: how add polymorphic comments feed?

development.log

started post "/comments" ::1 @ 2015-04-23 20:12:14 -0400  processing commentscontroller#create html    parameters: {"utf8"=>"✓", "authenticity_token"=>"hu1lxg2bmqsybo8j2sgieb4wz3ez5kz/e64mp6ssbwbnh+dddyttnqxy+iycluhhvs2wibxrtd5hqva5sgtxbg==", "comment"=>{"content"=>"test"}, "button"=>""}    [1m[35muser load (0.2ms)[0m  select  "users".* "users" "users"."id" = ? limit 1  [["id", 2]]    [1m[36mhabit load (0.1ms)[0m  [1mselect "habits".* "habits" "habits"."user_id" = ?[0m  [["user_id", 2]]    [1m[35mhabit load (2.5ms)[0m  select "habits".* "habits"    [1m[36mactsastaggableon::tag load (0.3ms)[0m  [1mselect "tags".* "tags" (lower(name) = lower('ingrain'))[0m    [1m[35mcomment load (0.3ms)[0m  select  "comments".* "comments" "comments"."id" = ? limit 1  [["id", nil]]  completed 404 not found in 16ms    activerecord::recordnotfound (couldn't find comment 'id'=):    app/controllers/comments_controller.rb:61:in `load_commentable'

thank much!

the problem is, work, looks setup comments nested resource of whatever you're commenting on in routes file.

so, if want comments on activities, you'd have:

resources :activites   resources :comments end 

this way, when #load_commentable method picks apart request path, it'll commentable , id first 2 segments.

it looks, instead, you're trying use comments top level resource.

update: when call partial, pass along url helper form should use. this:

<%= render "comments/form", new_comment: comment.new(commentable_id: activity.id, commentable_type: activity.class.model_name), create_url: :activity_comments_path %> 

then, on in partial, invoke helper , pass result url option - this:

<%= form_for new_comment, url: send(create_url, new_comment.commentable) 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -