1

I added this to my valuations _form:

<%= render "comments/comments" %>

<%= link_to_add_association 'Add Comment', f, :comments %>

<%= f.fields_for :comments do |comment| %>
  <%= render "comments/form_fields", :f => comment %>
<% end %>

Then when I click on "Add Comment" I'm given just a blank space. I'm trying to integrate polymorphic comments with nested_attributes.

<%= form_for [@commentable, @comment] do |f| %>
  <%= f.text_area :content, rows: 4, class: 'form-control', placeholder: 'Enter Comment' %>
  <%= button_tag(type: 'submit', class: "btn") do %>
    <span class="glyphicon glyphicon-plus"></span> Comment
  <% end %>
<% end %>

valuation.rb

has_many :comments, as: :commentable
accepts_nested_attributes_for :comments, :reject_if => :all_blank, :allow_destroy => true

comment.rb

class Comment < ActiveRecord::Base
  include PublicActivity::Common
  belongs_to :commentable, polymorphic: true
end

controllers

class CommentsController < ApplicationController
 before_action :load_commentable
  before_action :set_comment, only: [:show, :edit, :update, :destroy]
  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
   @comment.create_activity :create, owner: current_user 
   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 was updated."
  else
   render :edit
  end
 end

 def destroy
  @comment = current_user.comments.find(params[:id])
  @comment.destroy
  @comment.create_activity :destroy, owner: current_user
  redirect_to @commentable, notice: "comment destroyed."
 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)
 end

 def comment_params
  params.require(:comment).permit(:content, :commentable)
 end
end

class ValuationsController < ApplicationController
  before_action :set_valuation, only: [:show, :edit, :update, :destroy]
  before_action :logged_in_user, only: [:create, :destroy]

  def index
    if params[:tag]
      @valuations = Valuation.tagged_with(params[:tag])
    else
      @valuations = Valuation.order('RANDOM()')
    end
  end

  def show
    @valuation = Valuation.find(params[:id])
    @commentable = @valuation
    @comments = @commentable.comments
    @comment = Comment.new
  end

  def new
    @valuation = current_user.valuations.build
    @commentable = @valuation
    @comments = @commentable.comments
    @comment = Comment.new
  end

  def edit
  end

  def create
    @valuation = current_user.valuations.build(valuation_params)
    if @valuation.save
      redirect_to @valuation, notice: 'Value was successfully created'
    else
      @feed_items = []
      render 'pages/home'
  end
end

  def update
    if @valuation.update(valuation_params)
      redirect_to @valuation, notice: 'Value was successfully updated'
    else
      render action: 'edit'
  end
end

  def destroy
    @valuation.destroy
    redirect_to valuations_url
  end

private
    def set_valuation
      @valuation = Valuation.find(params[:id])
    end

    def correct_user
      @valuation = current_user.valuations.find_by(id: params[:id])
      redirect_to valuations_path, notice: "Not authorized to edit this valuation" if @valuation.nil?
    end

    def valuation_params
      params.require(:valuation).permit(:name, :private_submit, :tag_list, :content, :commentable, :comment)
    end
end

Thank you so much for your help. I was inspired to ask this question from this question: ActionController::UrlGenerationError in Valuations#new

Community
  • 1
  • 1
AnthonyGalli.com
  • 2,796
  • 5
  • 31
  • 80
  • You've not posted what appear to be the most relevant thing - your `link_to_add_association` method. CommentsController shouldn't be relevant here. – Frederick Cheung Mar 26 '15 at 18:10

0 Answers0