2013-08-11 1 views
2

"acts_as_commentable"보석을 사용하고 있습니다.하지만 "상태"에 대한 새 댓글을 만들려고하면이 오류가 발생합니다.Acts_as_commentable : AssociationTypeMismatch

enter image description here

컨트롤러

enter image description here

업데이트 :이 사용하는 경우

, 나는 오류를 얻을하지 않습니다하지만이에 코멘트를 저장하지 않습니다 데이터베이스

,442,

보기

enter image description here

<% if user_signed_in? %> 
<%=form_for([resource.user, resource]) do |f| %> 
    <%=f.fields_for :comments do |fc| %> 

     <div class="row"> 
      <div class="large-1 columns"> 
       <%=image_tag current_user.photo %> 
      </div> 

      <div class="large-11 columns text-left"> 

       <%=fc.text_area :comment, placeholder: "Write a comment..."%> 
       <div class="text-right"> 
        <%=fc.submit "Send", :class=>"pure-button pure-button-xsmall pure-button-primary" %> 
       </div> 

      </div> 
     </div> 
    <% end %> 
<% end %> 

모델 (상태)

class Status < ActiveRecord::Base 

    belongs_to :user 

    acts_as_commentable 
    acts_as_likeable 

     validates :message, :presence => true 
    end 

모델 (사용자),클래스 사용자 < 액티브 :: 자료

devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable  

    has_many :statuses 
    has_many :photos 
    has_many :videos 
    has_one :player_profile 

    mount_uploader :photo, ProfilePhotoUploader 

    accepts_nested_attributes_for :player_profile 


    acts_as_liker 

end 

Gemfile

source 'https://rubygems.org' 
ruby '2.0.0' 

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 
gem 'rails', '4.0.0' 
gem 'pg' 


gem 'sass-rails', git: 'https://github.com/rails/sass-rails.git' 

# Use Uglifier as compressor for JavaScript assets 
gem 'uglifier', '>= 1.3.0' 

# Use CoffeeScript for .js.coffee assets and views 
gem 'coffee-rails', git: 'git://github.com/rails/coffee-rails.git' 

# See https://github.com/sstephenson/execjs#readme for more supported runtimes 
# gem 'therubyracer', platforms: :ruby 

# Use jquery as the JavaScript library 
gem 'jquery-rails', '~> 3.0.0' 

# assets 

gem "asset_sync" 

gem 'zurb-foundation', :git => "https://github.com/zurb/foundation.git" 
gem "compass" 
gem 'compass-rails', '2.0.alpha.0' 
gem 'bourbon' 
gem "font-awesome-rails" 


gem 'kaminari' 

gem 'jquery-turbolinks', github: "kossnocorp/jquery.turbolinks" 
gem 'turbolinks', github: "rails/turbolinks" 

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 
gem 'jbuilder', '~> 1.0.1' 

group :doc do 
    # bundle exec rake doc:rails generates the API under doc/api. 
    gem 'sdoc', require: false 
end 

#gem 'protected_attributes' 
gem 'devise' #, "~> 3.0.0.rc",  github: 'plataformatec/devise' #, branch: 'rails4' 
gem 'responders',   github: 'plataformatec/responders' 
gem 'inherited_resources', github: 'josevalim/inherited_resources' 
gem 'ransack' 
gem 'activeadmin',   github: 'gregbell/active_admin', branch: 'rails4' 
gem 'formtastic',   github: 'justinfrench/formtastic' 

gem "nested_form" 
gem "simple_form" 
gem "mini_magick" 


gem 'omniauth' 
gem 'omniauth-facebook' 

gem 'carrierwave' 
gem "fog", "~> 1.3.1" 

gem 'public_activity' 
gem 'acts_as_commentable' 
gem "socialization" 

gem 'rails_12factor' 
+0

사용 '<% = f.fields_for : 어떻게 코멘트 | FC를 | %>', 단수 기호. – vee

+0

시도했지만, 얻을 : StatusesController # 업데이트 (알 수없는 특성 : 주석) ActiveRecord :: UnknownAttributeError http://cl.ly/image/1A1A1j2t0J1c – sparkle

+0

컨트롤러 코드를 게시하십시오. – vee

답변

3

중첩 모델

CommentController로 의견을 CommentController를 사용하는 대신 사용하여 해결이 해결

class CommentsController < ApplicationController 


    def create 
     comment = Comment.new comment_params 
     commentable = params[:comment][:commentable_type].constantize.find(params[:comment][:commentable_id]) 
     comment.commentable = commentable 
     comment.user = current_user 

     comment.save 


     redirect_to [commentable.user, commentable] 

    end 


    private 

    def comment_params 
     params.require(:comment).permit! 
    end 
end 

조회수

<%= form_for(Comment.new) do |f| %> 

<div class="row"> 
    <div class="large-1 columns"> 
     <%=image_tag current_user.photo %> 
    </div> 

    <div class="large-11 columns text-left"> 

      <%=f.text_area :comment, placeholder: "Write a comment..."%> 
      <%=f.hidden_field :commentable_type, :value => resource.class.to_s%> 
      <%=f.hidden_field :commentable_id, :value => resource.id%> 

     <div class="text-right"> 

      <%=f.submit "Send", :class=>"pure-button pure-button-xsmall pure-button-primary" %> 
     </div> 

    </div> 
</div> 

<% end %>