轨道 5 多态 注释控制器始终允许 = 假



Rails 5 多态注释模型,我正在使用 devise。

当我创建"新"注释时,允许的参数 => 总是假的;甚至我也使用 params.permit(:all(

class Comment < ApplicationRecord
    belongs_to :commentable, polymorphic: true
    has_many :comments, as: :commentable   
    default_scope ->{ order('created_at DESC') }   
    validates_presence_of :title
    validates_presence_of :description   
end

视图/评论/_form.html.erb

<%= form_with(model: comment, local: true) do |form| %> 

comments_controller.rb

    定义comment_params         params.fetch(:client, {}(           params.require(:comment(.permit(:title, :d escription(        params.permit(:all(        再见虫    结束

82:       params.fetch(:client, {})    
83:       params.require(:comment).permit(:title, :description)
84:       #params.permit(:title, :description, :client_id)
85:       params.permit(:all)
86:       byebug
=>  87:     end
88: 
89:    def find_commentable
90:      # @commentable = Client.find(params[:client_id]) 
if params[:client_id]    
91:      # for now

(byebug) params

我通过添加 :commentable_id 解决了我的问题,并将commentable_type添加到 comment_params((,因为 Comment 是多态模型。

定义comment_params
params.fetch(:client, {}(
params.require(:comment(.permit( :title, :d escription, :commentable_id, :commentable_type( 结束

谢谢你的帮助

最新更新