0

我不能正確的 - 我在我的應用程序中有多態關聯,它工作正常 - 我可以添加評論電影或導演通過軌道控制檯沒有問題。但使用窗體我得到錯誤「未初始化的常量Comment :: Commentable」。控制器和型號:多態協會 - 控制器創建錯誤

class CommentsController < ApplicationController 
    before_action :find_comment, only: [:edit, :update, :destroy] 
    before_action :logged_user 

    def new 
     if params[:movie_id] 
      @movie = Movie.find(params[:movie_id]) 
     else 
      @director = Director.find(params[:director_id]) 
     end 
     @comment = Comment.new 
    end 

    def create 
     if params[:movie_id] 
      @commentable = Movie.find(params[:movie_id]) 
     else 
      @commentable = Director.find(params[:director_id]) 
     end 
     @comment = @commentable.comments.new(comment_params) 
     @comment.user = current_user 
     if @comment.save 
      flash[:success] = 'Dodano komentarz.' 
      redirect_to @commentable 
     else 
      flash[:danger] = 'Coś poszło nie tak, spróbuj ponownie.' 
      render :new 
     end 
    end 

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

    def find_comment 
     @comment = Comment.find(params[:id]) 
    end 

end 

class Movie < ActiveRecord::Base 

    belongs_to :director 
    has_many :comments, as: :commentable, dependent: :destroy 
    has_many :users, through: :comments, source: :commentable, source_type: "User" 
    accepts_nested_attributes_for :comments, :allow_destroy => true 
end 

class User < ActiveRecord::Base 

    has_many :comments, dependent: :destroy 
    has_many :movies, through: :comments, source: :commentable, source_type: "Movie" 
    has_many :directors, through: :comments, source: :commentable, source_type: "Director" 
    accepts_nested_attributes_for :comments, :allow_destroy => true 
end 

class Comment < ActiveRecord::Base 
    belongs_to :commentable, polymorphic: true 
    belongs_to :user 

    validates_uniqueness_of :commentable, scope: :user 
    validates :content, presence: true, length: { minimum: 40 } 

end 

我將在稍後清理控制器,現在我只是想讓它工作。參數在請求中傳遞:

{"utf8"=>"✓", 
"authenticity_token"=>"l+uouiN7H6hHArtiHGSw4MCsA8d37b60oDgNfskrTpwlDmJigLfBuMzYbA37mweInZEniVjzAzoPKB0dBRvJAA==", 
"comment"=>{"grade"=>"1", 
"content"=>"Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, 
non felis. Maecenas malesuada elit lectus felis, 
malesuada ultricies. Curabitur et ligula. Ut molestie a, 
ultricies porta urna. Vestibulum commodo volutpat a, 
convallis ac, 
laoreet enim. Phasellus fermentum in, 
dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna. Vestibulum dapibus, 
mauris nec malesuada fames ac turpis velit, 
rhoncus eu, 
luctus"}, 
"commit"=>"Zapisz", 
"movie_id"=>"46"} 

我不知道爲什麼,但user_id沒有在請求中傳遞。你能幫我麼?我將不勝感激任何幫助!

編輯:發表評論 形式:

<%= render 'shared/errors', obj: @comment %>  
<div class="row"> 
    <div class="col-md-8 col-md-offset-2 well"> 
     <%= form_for @comment, url: new_or_edit do |f| %> 

      <%= f.label 'Ocena' %> 
      <%= f.select :grade, options_for_select(1..10) %> 

      <%= f.label 'Komentarz' %> 
      <%= f.text_area :content, rows: 5 %> 

      <%= f.submit 'Zapisz', class: 'btn btn-success' %> 
      <%= link_to 'Powrót', :back, class: 'btn btn-default uneditable-input' %> 
     <% end %> 
    </div> 
</div> 

模式:

create_table "comments", force: :cascade do |t| 
    t.string "content" 
    t.integer "grade" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.integer "commentable_id" 
    t.string "commentable_type" 
    t.integer "user_id" 
    end 
+0

你可以發表你的表單視圖? – SomeSchmo

+1

您可以發佈您的架構的評論以及整個堆棧跟蹤嗎? –

+0

爲了確保'commentable_id'和'commentable_type'列在那裏(或者運行並向我們顯示來自'rails console'的'Comment'的輸出),我建議發佈任何創建或影響Comment的遷移。 – TJR

回答

0

好它的工作原理。問題是線

validates_uniqueness_of :commentable, scope: :user 

它的工作正確地將其更改爲後:

validates :user, uniqueness: { scope: [ :commentable_type, :commentable_id ] }