我无法正确使用 - 我的应用程序中有多态协议且工作正常 - 我可以通过rails控制台为电影或导演添加注释而不会出现问题。但使用表单我得到错误“未初始化的常量评论::评论”。控制器和型号:
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 :(得分:0)
好的确有效。问题是行
validates_uniqueness_of :commentable, scope: :user
将其更改为:
后,它正常工作validates :user, uniqueness: { scope: [ :commentable_type, :commentable_id ] }