【问题标题】:Polymorphic association - controller create error多态关联 - 控制器创建错误
【发布时间】:2016-03-08 22:27:07
【问题描述】:

我做错了——我的应用程序中有多态关联,它运行良好——我可以通过 Rails 控制台将 cmets 添加到电影或导演中,没有问题。但是使用表单我得到错误“未初始化的常量 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

【问题讨论】:

  • 您可以发布您的表单视图吗?
  • 您能否发布您的 cmets 架构以及整个堆栈跟踪?
  • 我建议发布任何创建或影响评论的迁移,只是为了确保 commentable_idcommentable_type 列在那里(或运行并向我们展示 Comment 的输出来自rails console)
  • 我已经更新了帖子。 commentable_id 和 commentable_type 都存在。有什么想法吗?
  • 我没有提到“if @comment.save”会引发错误。

标签: ruby-on-rails ruby-on-rails-4 activerecord polymorphic-associations


【解决方案1】:

好的,它可以工作。问题是线

validates_uniqueness_of :commentable, scope: :user

改成之后就可以正常使用了:

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

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 2014-01-03
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多