0

我想以嵌套形式使用神社宝石,但发生了

不允许的参数::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

博客表单.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"}  

博客.rb

class Blog < ApplicationRecord
 belongs_to :user
 has_many :photos
 validates :title ,presence: true
 validates :content ,presence: true

end

照片.rb

 class Photo < ApplicationRecord
  belongs_to :blog
  belongs_to :user
  include ImageUploader[:image]

end

上传者.rb

   class ImageUploader < Shrine
    end

神社.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 Exception: undefined local variable or method `params' for #

我改变了 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>

Somwhow 我可以发送一个 user_id,我很欣赏 Fernand,Amit Patel,我在视图字段中使用 hidden_​​field 而不是合并方法

4

1 回答 1

0

那是因为您将 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)
  ...
于 2020-04-06T12:18:08.137 回答