STI and Polymorphic Association possible in rails 4? Not working for me -


i'm of newbie ruby on rails , went off of samurails.com single table inheritance rails 4 tutorial add different comment types. worked great problem i'm running when try use polymorphic associations comments , specific type function under other models such project , challenge. regular comment works, specific types not.

i haven't seen says how make work or option of going appreciated.

class comment < activerecord::base    has_merit   acts_as_votable    belongs_to :commentable, :polymorphic => true   belongs_to :user    belongs_to :commenttype   belongs_to :project    def self.types     %w(question idea problem)   end    def commentable_type=(stype)     super(stype.to_s.classify.constantize.base_class.to_s)   end    scope :questions, -> {where(type: 'question')}   scope :ideas, -> {where(type: 'idea')}   scope :problems, -> {where(type: 'problem')} end  class question < comment  end  class idea < comment  end  class problem < comment  end  class project < activerecord::base    belongs_to :user    has_many :comments, :as => :commentable, :class_name => "comment"   has_many :questions, :as => :commentable, :class_name => "question"   has_many :ideas, :as => :commentable, :class_name => "idea"   has_many :problems, :as => :commentable, :class_name => "problem"    delegate :questions, :ideas, :problems, to: :comments  end  class commentscontroller < applicationcontroller   before_action :set_commentable, only: [:index, :new, :create]   before_action :set_type   before_action :set_comment, only: [:show, :edit, :update, :destroy]    def index     @comments = type_class.all   end    def show   end    def new     @comment = type_class.new   end    def edit   end    def create        @comment = @commentable.comments.new(comment_params)       @comment.user = current_user       if @comment.save         redirect_to :back, notice: "#{type} added."       else         render action: 'new'       end   end    def update     if @comment.update(comment_params)       redirect_to @comment.commentable, notice: "#{type} updated."     else       render action: 'edit'     end   end    def destroy     @user = current_user     @comment = @commentable.comments.where(comment_user: current_user).first     @commentable.comment.destroy     respond_to |format|       format.html { redirect_to @commentable, notice: "comment deleted." }       format.js   end end  private  def set_comment   @comment = type_class.find(params[:id]) end  def set_type   @type = type end  def type   comment.types.include?(params[:type]) ? params[:type] : "comment" end  def type_class   type.constantize end  def set_commentable   @commentable = find_commentable end  # add more commentable models here def find_commentable   if params[:challenge_id]     challenge.find(params[:challenge_id])   else   end end  def find_commentable   if params[:project_id]     project.find(params[:project_id])   else   end end     def comment_params      params.require(type.underscore.to_sym).permit(:body, :type, :user_id, :commentable_id, :commentable_type, :commentable, :comment_type)    end  end  module commentshelper    def sti_comment_path(type = "comment", comment = nil, action = nil)     send "#{format_sti(action, type, comment)}_path", comment   end    def format_sti(action, type, comment)     action || comment ? "#{format_action(action)}#{type.underscore}" : "#{type.underscore.pluralize}"   end    def format_action(action)     action ? "#{action}_" : ""   end  end  <%= form_for [commentable, comment.new], :html => { :multipart => true } |f| %>  <%= f.text_area :body, class: "form-control", placeholder: "what's on mind?" %>  <%= f.label :type %><br> <%= f.select :type, comment.types.map {|r| [r.humanize, r.camelcase]}, {}, disabled: @type != "comment" %>       <%= f.submit "post", class: "btn pull-right" %> 


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -