Ruby on rails 传递多个具有嵌套属性和多态关联的图像
我正在尝试传递多个图像,这些图像具有嵌套属性和多态关联。但是得到这个错误Ruby on rails 传递多个具有嵌套属性和多态关联的图像,ruby-on-rails,ruby-on-rails-4,ruby-on-rails-5,polymorphic-associations,Ruby On Rails,Ruby On Rails 4,Ruby On Rails 5,Polymorphic Associations,我正在尝试传递多个图像,这些图像具有嵌套属性和多态关联。但是得到这个错误没有将符号隐式转换为整数。虽然我知道我已经完成了所有的设置,但我不知道我遗漏了什么。我使用了载波波上传图像 User.rb class User < ApplicationRecord has_many :images,-> { where(object_type: 'User') },as: :object,:foreign_key => 'object_id' ,dependent: :des
没有将符号隐式转换为整数
。虽然我知道我已经完成了所有的设置,但我不知道我遗漏了什么。我使用了载波波
上传图像
User.rb
class User < ApplicationRecord
has_many :images,-> { where(object_type: 'User') },as: :object,:foreign_key => 'object_id' ,dependent: :destroy
accepts_nested_attributes_for :images
validates :first_name,presence: true
end
class Image < ApplicationRecord
before_destroy :remember_id
after_destroy :remove_id_directory
mount_uploader :image, ImageUploader
belongs_to :object,polymorphic: true
validates :name,presence: true
protected
def remember_id
@id = id
end
def remove_id_directory
FileUtils.remove_dir("#{Rails.root}/public/uploads/image/image/#{@id}", :force => true)
end
end
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
@user.images.build
end
def create
@user = User.new
@user.images.build(user_params)
if @user.save
redirect_to users_path
else
render :new
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name,images_attributes: [:name,:image,:user_id ])
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images_attributes do |images_fields| %>
Nama : <%= images_fields.text_field :name %>
Image: <%= images_fields.file_field :image,:multiple => true %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
def create
@user = User.new(user_params)
if @user.save
params[:images_attributes]['image'].each do |a|
@image_attachment = @user.images.create!(:image => a,:name=> params[:images_attributes][:name].join)
end
redirect_to users_path
else
render :new
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images do |images_fields| %>
Name : <%= images_fields.text_field :name,name: "images_attributes[name][]" %>
Image: <%= images_fields.file_field :image,:multiple => true,name: "images_attributes[image][]" %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
class用户{where(object_type:'User')},as::object,:foreign_key=>'object_id',dependent::destroy
接受图像的\u嵌套\u属性\u
验证:名字,状态:true
结束
Image.rb
class User < ApplicationRecord
has_many :images,-> { where(object_type: 'User') },as: :object,:foreign_key => 'object_id' ,dependent: :destroy
accepts_nested_attributes_for :images
validates :first_name,presence: true
end
class Image < ApplicationRecord
before_destroy :remember_id
after_destroy :remove_id_directory
mount_uploader :image, ImageUploader
belongs_to :object,polymorphic: true
validates :name,presence: true
protected
def remember_id
@id = id
end
def remove_id_directory
FileUtils.remove_dir("#{Rails.root}/public/uploads/image/image/#{@id}", :force => true)
end
end
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
@user.images.build
end
def create
@user = User.new
@user.images.build(user_params)
if @user.save
redirect_to users_path
else
render :new
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name,images_attributes: [:name,:image,:user_id ])
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images_attributes do |images_fields| %>
Nama : <%= images_fields.text_field :name %>
Image: <%= images_fields.file_field :image,:multiple => true %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
def create
@user = User.new(user_params)
if @user.save
params[:images_attributes]['image'].each do |a|
@image_attachment = @user.images.create!(:image => a,:name=> params[:images_attributes][:name].join)
end
redirect_to users_path
else
render :new
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images do |images_fields| %>
Name : <%= images_fields.text_field :name,name: "images_attributes[name][]" %>
Image: <%= images_fields.file_field :image,:multiple => true,name: "images_attributes[image][]" %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
类映像true)
结束
结束
用户\u控制器.rb
class User < ApplicationRecord
has_many :images,-> { where(object_type: 'User') },as: :object,:foreign_key => 'object_id' ,dependent: :destroy
accepts_nested_attributes_for :images
validates :first_name,presence: true
end
class Image < ApplicationRecord
before_destroy :remember_id
after_destroy :remove_id_directory
mount_uploader :image, ImageUploader
belongs_to :object,polymorphic: true
validates :name,presence: true
protected
def remember_id
@id = id
end
def remove_id_directory
FileUtils.remove_dir("#{Rails.root}/public/uploads/image/image/#{@id}", :force => true)
end
end
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
@user.images.build
end
def create
@user = User.new
@user.images.build(user_params)
if @user.save
redirect_to users_path
else
render :new
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name,images_attributes: [:name,:image,:user_id ])
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images_attributes do |images_fields| %>
Nama : <%= images_fields.text_field :name %>
Image: <%= images_fields.file_field :image,:multiple => true %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
def create
@user = User.new(user_params)
if @user.save
params[:images_attributes]['image'].each do |a|
@image_attachment = @user.images.create!(:image => a,:name=> params[:images_attributes][:name].join)
end
redirect_to users_path
else
render :new
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images do |images_fields| %>
Name : <%= images_fields.text_field :name,name: "images_attributes[name][]" %>
Image: <%= images_fields.file_field :image,:multiple => true,name: "images_attributes[image][]" %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
class UsersController
users/new.html.erb
class User < ApplicationRecord
has_many :images,-> { where(object_type: 'User') },as: :object,:foreign_key => 'object_id' ,dependent: :destroy
accepts_nested_attributes_for :images
validates :first_name,presence: true
end
class Image < ApplicationRecord
before_destroy :remember_id
after_destroy :remove_id_directory
mount_uploader :image, ImageUploader
belongs_to :object,polymorphic: true
validates :name,presence: true
protected
def remember_id
@id = id
end
def remove_id_directory
FileUtils.remove_dir("#{Rails.root}/public/uploads/image/image/#{@id}", :force => true)
end
end
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
@user.images.build
end
def create
@user = User.new
@user.images.build(user_params)
if @user.save
redirect_to users_path
else
render :new
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name,images_attributes: [:name,:image,:user_id ])
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images_attributes do |images_fields| %>
Nama : <%= images_fields.text_field :name %>
Image: <%= images_fields.file_field :image,:multiple => true %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
def create
@user = User.new(user_params)
if @user.save
params[:images_attributes]['image'].each do |a|
@image_attachment = @user.images.create!(:image => a,:name=> params[:images_attributes][:name].join)
end
redirect_to users_path
else
render :new
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images do |images_fields| %>
Name : <%= images_fields.text_field :name,name: "images_attributes[name][]" %>
Image: <%= images_fields.file_field :image,:multiple => true,name: "images_attributes[image][]" %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
非农产品市场准入:
图像:真%>
日志
Started POST "/users" for 127.0.0.1 at 2017-01-27 11:20:29 +0530
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wpb1cqph+SAucEfeb0isx7DKtsV4PQeyq47xZbz/Ac7cSfoSleBXynNJiT+kNni5OaX/DqNhR+h1Xvli2QyBbg==", "user"=>{"first_name"=>"adasd", "images_attributes"=>{"0"=>{"name"=>"asdasd", "image"=>[#<ActionDispatch::Http::UploadedFile:0x00000003819838 @tempfile=#<Tempfile:/tmp/RackMultipart20170127-3305-16cl68l.png>, @original_filename="1.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[images_attributes][0][image][]\"; filename=\"1.png\"\r\nContent-Type: image/png\r\n">, #<ActionDispatch::Http::UploadedFile:0x000000038197e8 @tempfile=#<Tempfile:/tmp/RackMultipart20170127-3305-8o6vn7.png>, @original_filename="27_dec.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[images_attributes][0][image][]\"; filename=\"27_dec.png\"\r\nContent-Type: image/png\r\n">, #<ActionDispatch::Http::UploadedFile:0x000000038196a8 @tempfile=#<Tempfile:/tmp/RackMultipart20170127-3305-1j7hl1r.png>, @original_filename="Screenshot from 2017-01-13 16:52:49.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"user[images_attributes][0][image][]\"; filename=\"Screenshot from 2017-01-13 16:52:49.png\"\r\nContent-Type: image/png\r\n">]}}}, "commit"=>"Submit"}
Unpermitted parameter: image
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.0ms)
2017年1月27日11:20:29+0530,127.0.0.1版开始发布“/用户”
由UsersController处理#创建为HTML
参数:{“utf8”=>“✓", "真实性令牌“=>”wpb1cqph+SAucEfeb0isx7DKtsV4PQeyq47xZbz/ac7csfoslebxynjit+kni5oax/DqNhR+h1Xvli2QyBbg==”,“用户”=>{“第一名”=>“adasd”,“图像属性”=>{“0”=>{“名称”=>“asdasd”,“图像”=>,[\35;、\ 35;、\ 35;、\ 35;]}}>,“提交”}
不允许的参数:图像
在4ms内完成500个内部服务器错误(ActiveRecord:0.0ms)
我找到了解决办法 用户\u控制器.rb
class User < ApplicationRecord
has_many :images,-> { where(object_type: 'User') },as: :object,:foreign_key => 'object_id' ,dependent: :destroy
accepts_nested_attributes_for :images
validates :first_name,presence: true
end
class Image < ApplicationRecord
before_destroy :remember_id
after_destroy :remove_id_directory
mount_uploader :image, ImageUploader
belongs_to :object,polymorphic: true
validates :name,presence: true
protected
def remember_id
@id = id
end
def remove_id_directory
FileUtils.remove_dir("#{Rails.root}/public/uploads/image/image/#{@id}", :force => true)
end
end
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
@user.images.build
end
def create
@user = User.new
@user.images.build(user_params)
if @user.save
redirect_to users_path
else
render :new
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name,images_attributes: [:name,:image,:user_id ])
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images_attributes do |images_fields| %>
Nama : <%= images_fields.text_field :name %>
Image: <%= images_fields.file_field :image,:multiple => true %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
def create
@user = User.new(user_params)
if @user.save
params[:images_attributes]['image'].each do |a|
@image_attachment = @user.images.create!(:image => a,:name=> params[:images_attributes][:name].join)
end
redirect_to users_path
else
render :new
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images do |images_fields| %>
Name : <%= images_fields.text_field :name,name: "images_attributes[name][]" %>
Image: <%= images_fields.file_field :image,:multiple => true,name: "images_attributes[image][]" %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
users/new.html.erb
class User < ApplicationRecord
has_many :images,-> { where(object_type: 'User') },as: :object,:foreign_key => 'object_id' ,dependent: :destroy
accepts_nested_attributes_for :images
validates :first_name,presence: true
end
class Image < ApplicationRecord
before_destroy :remember_id
after_destroy :remove_id_directory
mount_uploader :image, ImageUploader
belongs_to :object,polymorphic: true
validates :name,presence: true
protected
def remember_id
@id = id
end
def remove_id_directory
FileUtils.remove_dir("#{Rails.root}/public/uploads/image/image/#{@id}", :force => true)
end
end
class UsersController < ApplicationController
def index
@users = User.all
end
def new
@user = User.new
@user.images.build
end
def create
@user = User.new
@user.images.build(user_params)
if @user.save
redirect_to users_path
else
render :new
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:first_name,images_attributes: [:name,:image,:user_id ])
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images_attributes do |images_fields| %>
Nama : <%= images_fields.text_field :name %>
Image: <%= images_fields.file_field :image,:multiple => true %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
def create
@user = User.new(user_params)
if @user.save
params[:images_attributes]['image'].each do |a|
@image_attachment = @user.images.create!(:image => a,:name=> params[:images_attributes][:name].join)
end
redirect_to users_path
else
render :new
end
end
<%= form_for @user,html: {multipart: :true} do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :images do |images_fields| %>
Name : <%= images_fields.text_field :name,name: "images_attributes[name][]" %>
Image: <%= images_fields.file_field :image,:multiple => true,name: "images_attributes[image][]" %>
<% end %>
<%=f.submit "Submit" %>
<% end %>
姓名:
Image:true,name:“images\u attributes[Image][]”%>
Hi。如果您查看日志(在控制台窗口或log/development.log
中),找到与该错误消息匹配的stacktrace并将其复制/粘贴到您的问题中,我们将帮助您。这将帮助我们找出哪一行(不仅在您的代码中,而且在carrierwave中)是导致该错误的原因。但只是一个快速的哑炮也告诉我,这可能不太正确:f.fields\u for:images\u attributes
它应该是:f.fields\u for:images
(在表单中。在permit/require-else中使用images\u attributes
)好的,我向你展示我的开发。logTaryn East-它不起作用,我尝试与f.fields_合作:images@AniketShivamTiwari:请告诉我错误。别忘了在属性images\u属性中添加id:[:id,:name,:image,:user\u id]