【问题标题】:rails 5 polymorphic Comment controller always permitted = falserails 5 多态注释控制器始终允许 = false
【发布时间】:2018-09-21 12:40:10
【问题描述】:

Rails 5 多态评论模型,我正在使用设计。

当我创建“新”评论时,允许的参数 => 总是错误的;即使我使用 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

views/cmets/_form.html.erb

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

cmets_controller.rb

def 注释参数 params.fetch(:client, {}) params.require(:comment).permit(:title, :description) 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

【问题讨论】:

  • 您能否将您的comments#create 操作添加到您的问题中?

标签: parameters permissions ruby-on-rails-5


【解决方案1】:

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

定义注释参数
params.fetch(:client, {})
params.require(:comment).permit(:title, :description, :commentable_id, :commentable_type) 结束

感谢您的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    相关资源
    最近更新 更多