不允许的参数::photos_attributes
Unpermitted parameter: :photos_attributes
我想以嵌套形式使用 shrine gem,
但是发生了
Unpermitted parameter: :photos_attributes
如何解决?
我尝试了一些方法,
def blog_form_params
params.require(:blog_form).permit(:title , :content ).merge!(user_id: current_user.id , :photos_attributes =>[:image, :id ,:destroy ])
end
和
def blog_form_params
params.require(:blog_form).permit(:title , :content ).merge!(user_id: current_user.id , photos_attributes: {:image, :id ,:destroy })
end
等等,但我无法解决这个错误是什么问题?
请告诉我如何解决它
强参数
def blog_form_params
params.require(:blog_form).permit(:title , :content ).merge!(user_id:
current_user.id ,
photos_attributes: [:image, :id ,:destroy ])
end
BlogForm.rb
class BlogForm
include ActiveModel::Model
attr_accessor :title, :content, :user_id , :photos
def blog_builder
@user = User.find(user_id)
@blogs = @user.blogs.create(title: title , content: content )
return @blogs
end
concerning :PhotosBuilder do
attr_reader :photos_attributes
def photos
@blogs = blog_builder
@photos ||= @blogs.photos.new
end
def photos_attributes=(attributes)
@blogs = blog_builder
@photos ||= @blogs.photos.new(attributes)
end
end
def build_association
@user.photos << @photos if @photos != nil
end
def save
return false if invalid?
blog_builder
@blogs.save
@photos.save if @photos != nil
build_association
end
end
发送参数
{"authenticity_token"=>"qDssddHt4EhfFWN33SPMk2F5fcvPToh143hrwilw0p98HK2RMEwdKDF7QsGYiSQ7AQHN02lpdBaCbSAed80Swg==",
"blog_form"=>{"title"=>"", "content"=>"sample", "photos_attributes"=>
{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007f92408f07a8 @
tempfile=#<Tempfile:/tmp/RackMultipart20200406-4102-1x3utj1.png>,
@original_filename="p8-1.png", @content_type="image/png",
@headers="Content-Disposition: form-data;
name=\"blog_form[photos_attributes][image]\"; filename=\"p8-
1.png\"\r\nContent-Type: image/png\r\n">}}, "commit"=>"create",
"user_id"=>"2"}
blog.rb
class Blog < ApplicationRecord
belongs_to :user
has_many :photos
validates :title ,presence: true
validates :content ,presence: true
end
photo.rb
class Photo < ApplicationRecord
belongs_to :blog
belongs_to :user
include ImageUploader[:image]
end
uploader.rb
class ImageUploader < Shrine
end
shrine.rb
require "shrine/storage/file_system"
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/store"), # permanent
}
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
blogs_controller
def new
@blog_form = BlogForm.new
end
def create
@blog_form = BlogForm.new(blog_form_params)
if @blog_form.save
redirect_to user_blogs_path
else
#False action
end
end
def blog_form_params
params.require(:blog_form).permit(:title , :content, photos_attributes:
[:image] ).merge!(user_id: current_user.id)
end
我可以解决这个问题,
相反,我无法合并 user_id
然后发生了 ActiveRecord::RecordNotFound(找不到没有 ID 的用户):
def blog_builder
@user = User.find(user_id)#Here is a new problem
@blogs = @user.blogs.create(title: title , content: content )
return @blogs
end
也许我可以发送参数
(byebug) blog_form_params
<ActionController::Parameters {"title"=>"", "content"=>"sample",
"photos_attributes"=><ActionController::Parameters {"image"=>#
<ActionDispatch::Http::UploadedFile:0x00007f9240bc77b0 @tempfile=#
<Tempfile:/tmp/RackMultipart20200406-4102-rc9eo6.png>,
@original_filename="p8-1.png", @content_type="image/png",
@headers="Content-Disposition: form-data;
name=\"blog_form[photos_attributes][image]\"; filename=\"p8-
1.png\"\r\nContent-Type: image/png\r\n">} permitted: true>, "user_id"=>2}
permitted: true>
所以错误归因于表单对象
我上传了blog_form.rb
def blog_builder
@user = User.find(params[:user_id])#previously User.find(user_id)
@blogs = @user.blogs.create(title: title , content: content )
return @blogs
end
不起作用,我必须 (byebug) params[:user_id]
*** NameError 异常:#
的未定义局部变量或方法“params”
无
我更改了 user_id 而不是 params[:user_id]
这样
(byebug) @blog_form.user_id=params[:user_id]
NoMethodError Exception: undefined method `user_id=' for nil:NilClass
nil
创建
def create
@blog_form = BlogForm.new(blog_form_params)
@blog_form.user_id=params[:user_id]
if @blog_form.save
Blog_form
def blog_builder
@user = User.find(user_id)
@blogs = @user.blogs.create(title: title , content: content )
return @blogs
end
blog_form_params
(byebug) blog_form_params
<ActionController::Parameters {"title"=>"title", "content"=>"sample",
"photos_attributes"=><ActionController::Parameters {"image"=>#
<ActionDispatch::Http::UploadedFile:0x00007f80f5209680 @tempfile=#
<Tempfile:/tmp/RackMultipart20200407-10555-iewcks.png>,
@original_filename="p8-1.png", @content_type="image/png",
@headers="Content-Disposition: form-data;
name=\"blog_form[photos_attributes][image]\"; filename=\"p8-
1.png\"\r\nContent-Type: image/png\r\n">} permitted: true>, "user_id"=>2}
permitted: true>
我怎么能发一封 user_id,感谢 Fernand、Amit Patel,
我在视图中使用 hidden_field 而不是合并方法
那是因为您将 photos_attributes 放在合并部分而不是 blog_form 属性
这个怎么样:
params.require(:blog_form).permit(:title , :content, :photos_attributes =>[:image, :id, :destroy] )
然后更新 blogs_controller:
def create
@blog_form = BlogForm.new(blog_form_params)
@blog_form.user_id=params[:user_id]
...
然后在 BlogForm.rb:
def blog_builder
@user = User.find(user_id)
...
我想以嵌套形式使用 shrine gem, 但是发生了
Unpermitted parameter: :photos_attributes
如何解决? 我尝试了一些方法,
def blog_form_params
params.require(:blog_form).permit(:title , :content ).merge!(user_id: current_user.id , :photos_attributes =>[:image, :id ,:destroy ])
end
和
def blog_form_params
params.require(:blog_form).permit(:title , :content ).merge!(user_id: current_user.id , photos_attributes: {:image, :id ,:destroy })
end
等等,但我无法解决这个错误是什么问题? 请告诉我如何解决它
强参数
def blog_form_params
params.require(:blog_form).permit(:title , :content ).merge!(user_id:
current_user.id ,
photos_attributes: [:image, :id ,:destroy ])
end
BlogForm.rb
class BlogForm
include ActiveModel::Model
attr_accessor :title, :content, :user_id , :photos
def blog_builder
@user = User.find(user_id)
@blogs = @user.blogs.create(title: title , content: content )
return @blogs
end
concerning :PhotosBuilder do
attr_reader :photos_attributes
def photos
@blogs = blog_builder
@photos ||= @blogs.photos.new
end
def photos_attributes=(attributes)
@blogs = blog_builder
@photos ||= @blogs.photos.new(attributes)
end
end
def build_association
@user.photos << @photos if @photos != nil
end
def save
return false if invalid?
blog_builder
@blogs.save
@photos.save if @photos != nil
build_association
end
end
发送参数
{"authenticity_token"=>"qDssddHt4EhfFWN33SPMk2F5fcvPToh143hrwilw0p98HK2RMEwdKDF7QsGYiSQ7AQHN02lpdBaCbSAed80Swg==",
"blog_form"=>{"title"=>"", "content"=>"sample", "photos_attributes"=>
{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007f92408f07a8 @
tempfile=#<Tempfile:/tmp/RackMultipart20200406-4102-1x3utj1.png>,
@original_filename="p8-1.png", @content_type="image/png",
@headers="Content-Disposition: form-data;
name=\"blog_form[photos_attributes][image]\"; filename=\"p8-
1.png\"\r\nContent-Type: image/png\r\n">}}, "commit"=>"create",
"user_id"=>"2"}
blog.rb
class Blog < ApplicationRecord
belongs_to :user
has_many :photos
validates :title ,presence: true
validates :content ,presence: true
end
photo.rb
class Photo < ApplicationRecord
belongs_to :blog
belongs_to :user
include ImageUploader[:image]
end
uploader.rb
class ImageUploader < Shrine
end
shrine.rb
require "shrine/storage/file_system"
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/store"), # permanent
}
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
blogs_controller
def new
@blog_form = BlogForm.new
end
def create
@blog_form = BlogForm.new(blog_form_params)
if @blog_form.save
redirect_to user_blogs_path
else
#False action
end
end
def blog_form_params
params.require(:blog_form).permit(:title , :content, photos_attributes:
[:image] ).merge!(user_id: current_user.id)
end
我可以解决这个问题, 相反,我无法合并 user_id 然后发生了 ActiveRecord::RecordNotFound(找不到没有 ID 的用户):
def blog_builder
@user = User.find(user_id)#Here is a new problem
@blogs = @user.blogs.create(title: title , content: content )
return @blogs
end
也许我可以发送参数
(byebug) blog_form_params
<ActionController::Parameters {"title"=>"", "content"=>"sample",
"photos_attributes"=><ActionController::Parameters {"image"=>#
<ActionDispatch::Http::UploadedFile:0x00007f9240bc77b0 @tempfile=#
<Tempfile:/tmp/RackMultipart20200406-4102-rc9eo6.png>,
@original_filename="p8-1.png", @content_type="image/png",
@headers="Content-Disposition: form-data;
name=\"blog_form[photos_attributes][image]\"; filename=\"p8-
1.png\"\r\nContent-Type: image/png\r\n">} permitted: true>, "user_id"=>2}
permitted: true>
所以错误归因于表单对象
我上传了blog_form.rb
def blog_builder
@user = User.find(params[:user_id])#previously User.find(user_id)
@blogs = @user.blogs.create(title: title , content: content )
return @blogs
end
不起作用,我必须 (byebug) params[:user_id] *** NameError 异常:#
的未定义局部变量或方法“params”无
我更改了 user_id 而不是 params[:user_id] 这样
(byebug) @blog_form.user_id=params[:user_id]
NoMethodError Exception: undefined method `user_id=' for nil:NilClass
nil
创建
def create
@blog_form = BlogForm.new(blog_form_params)
@blog_form.user_id=params[:user_id]
if @blog_form.save
Blog_form
def blog_builder
@user = User.find(user_id)
@blogs = @user.blogs.create(title: title , content: content )
return @blogs
end
blog_form_params
(byebug) blog_form_params
<ActionController::Parameters {"title"=>"title", "content"=>"sample",
"photos_attributes"=><ActionController::Parameters {"image"=>#
<ActionDispatch::Http::UploadedFile:0x00007f80f5209680 @tempfile=#
<Tempfile:/tmp/RackMultipart20200407-10555-iewcks.png>,
@original_filename="p8-1.png", @content_type="image/png",
@headers="Content-Disposition: form-data;
name=\"blog_form[photos_attributes][image]\"; filename=\"p8-
1.png\"\r\nContent-Type: image/png\r\n">} permitted: true>, "user_id"=>2}
permitted: true>
我怎么能发一封 user_id,感谢 Fernand、Amit Patel, 我在视图中使用 hidden_field 而不是合并方法
那是因为您将 photos_attributes 放在合并部分而不是 blog_form 属性
这个怎么样:
params.require(:blog_form).permit(:title , :content, :photos_attributes =>[:image, :id, :destroy] )
然后更新 blogs_controller:
def create
@blog_form = BlogForm.new(blog_form_params)
@blog_form.user_id=params[:user_id]
...
然后在 BlogForm.rb:
def blog_builder
@user = User.find(user_id)
...