- 首页
- 其他开发
- 多个回形针文件上传Rails4上的不允许的参数
多个回形针文件上传Rails4上的不允许的参数
[英] Unpermitted parameters on multiple paperclip file uploads Rails4
本文介绍了多个回形针文件上传Rails4上的不允许的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有吉他"和照片"模型的 ruby 2.0.0 和 rails 4.0.0 应用程序.我已经使用回形针在 Rails3 中上传单个文件,但我是在 Rails 4 中上传多个文件的新手.我创建了第二个模型来保存所述照片,并阅读强参数等.当我收到错误时尝试将 3 张照片添加到吉他.在日志中:不允许的参数:photos_attributes".我试过将 photos_attributes 添加到白名单中,但没有任何乐趣.我在这里拉我的头发 - 网络视图中没有错误,但是当我输入Photo.all"并输入Photo.all"时,我什么也没得到.我究竟做错了什么?我是新手,请多多关照.
吉他.rb
class 吉他 <ActiveRecord::Base归属地:用户has_many : 照片accepts_nested_attributes_for :photos结尾
照片.rb
班级照片',正方形:'200x200#',介质:'300x300>',大:'600x6003'}结尾
guitars_controller.rb
class GuitarsController <应用控制器before_action :set_guitar, only: [:show, :edit, :update, :destroy]# 获取/吉他# 获取/guitars.json定义索引@guitars = Guitar.all结尾# 获取/guitars/1# 获取/guitars/1.json高清秀结尾# 获取/吉他/新定义新@guitar = current_user.guitars.build3.times {@guitar.photos.build}结尾# 获取/guitars/1/edit定义编辑3.times {@guitar.photos.build}结尾# POST/吉他# POST/guitars.json定义创建@guitar = current_user.guitars.build(guitar_params)response_to do |格式|如果@guitar.saveformat.html { redirect_to @guitar,注意:'吉他已成功创建.'}format.json { 渲染动作:'show',状态::已创建,位置:@guitar }别的format.html { 渲染动作:'新' }format.json { 渲染 json: @guitar.errors, 状态: :unprocessable_entity }结尾结尾结尾# PATCH/PUT/guitars/1# PATCH/PUT/guitars/1.json定义更新response_to do |格式|如果@guitar.update(guitar_params)format.html { redirect_to @guitar,注意:'吉他已成功更新.'}format.json { 头:no_content }别的format.html { 渲染动作:'编辑' }format.json { 渲染 json: @guitar.errors, 状态: :unprocessable_entity }结尾结尾结尾# 删除/guitars/1# 删除/guitars/1.json销毁@guitar.destroyresponse_to do |格式|format.html { redirect_to Guitars_url }format.json { 头:no_content }结尾结尾私人的# 使用回调在动作之间共享公共设置或约束.def set_guitar@guitar = Guitar.find(params[:id])结尾# 永远不要相信来自可怕互联网的参数,只允许白名单通过.defguitar_paramsparams.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :oneowner, :user_id, :description, :照片_属性)结尾结尾
views/guitar/_form.html.erb
<%= form_for @guitar, :html =>{ :multipart =>真 } 做 |f|%><% if @guitar.errors.any?%><div id="error_explanation"><h2><%=pluralize(@guitar.errors.count, "error") %>禁止保存这把吉他:</h2><ul><% @guitar.errors.full_messages.each 做 |msg|%><li><%=msg%></li><%结束%>
<%结束%><div class="field"><%= f.label :make %><br><%= f.text_field :make %>
<div class="field"><%= f.label :model %><br><%= f.text_field :model %>
<div class="field"><%= f.label :year %><br><%= f.text_field :year %>
<div class="field"><%= f.label :color %><br><%= f.text_field :color %>
<div class="field"><%= f.label :serial %><br><%= f.text_field :serial %>
<div class="field"><%= f.label :price %><br><%= f.text_field :price %>
<div class="field"><%= f.label :condition %><br><%= f.number_field :condition %>
<div class="field"><%= f.label :kind %><br><%= f.text_field :kind %>
<div class="field"><%= f.label :bodykind %><br><%= f.text_field :bodykind %>
<div class="field"><%= f.label :frets %><br><%= f.text_field :frets %>
<div class="field"><%= f.label :oneowner %><br><%= f.check_box :oneowner %>
<div class="field"><%= f.label :extended_description %><br><%= f.text_area :描述%>
<%= f.fields_for :photos do |builder|%><%= builder.label :photo, "图像文件" %><%= builder.file_field :photo %><%结束%><div class="actions"><%= f.submit %>
<%结束%>
db/schema.rb
ActiveRecord::Schema.define(version: 20131014195859) docreate_table "guitars", force: true do |t|t.string "make"t.string模型"t.string "年"t.string "颜色"t.string "串行"t.string "价格"t.整数条件"t.string "种类"t.string "bodykind"t.string烦恼"t.boolean "oneowner"t.integer "user_id"t.datetime "created_at"t.datetime "updated_at"t.string "描述"结尾add_index "guitars", ["user_id"], 名称:"index_guitars_on_user_id",使用::btreecreate_table "photos", force: true do |t|t.integer "guitar_id"t.datetime "created_at"t.datetime "updated_at"t.string "photo_file_name"t.string "photo_content_type"t.integer "photo_file_size"t.datetime "photo_updated_at"结尾create_table "users", force: true do |t|t.string "email", 默认: "", null: falset.string "encrypted_password", 默认值: "", null: falset.string "reset_password_token"t.datetime "reset_password_sent_at"t.datetime "remember_created_at"t.integer "sign_in_count",默认值:0,空值:falset.datetime "current_sign_in_at"t.datetime "last_sign_in_at"t.string "current_sign_in_ip"t.string "last_sign_in_ip"t.datetime "created_at"t.datetime "updated_at"t.string "城市"t.string 状态"结尾add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btreeadd_index "users", ["reset_password_token"], 名称:"index_users_on_reset_password_token",唯一:true,使用::btree结尾
解决方案
有一个错字.
:photos_attributes =>[:照片]
I've got a ruby 2.0.0 and rails 4.0.0 app that has a 'Guitar' and 'Photos' model. I've used paperclip to upload a single file in Rails3, but am new to uploading multiple files in Rails 4. I created the second model to hold said photos, and read up on strong parameters, etc. I'm getting an error when attempting to add 3 photos to a guitar. In the logs: "Unpermitted parameters: photos_attributes". I've tried adding photos_attributes to the whitelist and no joy. I'm pulling my hair out here - there's no error in the web view, but when I console in and type 'Photo.all', I get nothing. What am I doing wrong? I'm kind of a newbie, please be gentle.
guitar.rb
class Guitar < ActiveRecord::Base
belongs_to :user
has_many :photos
accepts_nested_attributes_for :photos
end
photo.rb
class Photo < ActiveRecord::Base
belongs_to :guitar
has_attached_file :photo, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>',
large: '600x6003'
}
end
guitars_controller.rb
class GuitarsController < ApplicationController
before_action :set_guitar, only: [:show, :edit, :update, :destroy]
# GET /guitars
# GET /guitars.json
def index
@guitars = Guitar.all
end
# GET /guitars/1
# GET /guitars/1.json
def show
end
# GET /guitars/new
def new
@guitar = current_user.guitars.build
3.times {@guitar.photos.build}
end
# GET /guitars/1/edit
def edit
3.times {@guitar.photos.build}
end
# POST /guitars
# POST /guitars.json
def create
@guitar = current_user.guitars.build(guitar_params)
respond_to do |format|
if @guitar.save
format.html { redirect_to @guitar, notice: 'Guitar was successfully created.' }
format.json { render action: 'show', status: :created, location: @guitar }
else
format.html { render action: 'new' }
format.json { render json: @guitar.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /guitars/1
# PATCH/PUT /guitars/1.json
def update
respond_to do |format|
if @guitar.update(guitar_params)
format.html { redirect_to @guitar, notice: 'Guitar was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @guitar.errors, status: :unprocessable_entity }
end
end
end
# DELETE /guitars/1
# DELETE /guitars/1.json
def destroy
@guitar.destroy
respond_to do |format|
format.html { redirect_to guitars_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_guitar
@guitar = Guitar.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def guitar_params
params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :oneowner, :user_id, :description, :photos_attributes)
end
end
views/guitar/_form.html.erb
<%= form_for @guitar, :html => { :multipart => true } do |f| %>
<% if @guitar.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@guitar.errors.count, "error") %> prohibited this guitar from being saved:</h2>
<ul>
<% @guitar.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :make %><br>
<%= f.text_field :make %>
</div>
<div class="field">
<%= f.label :model %><br>
<%= f.text_field :model %>
</div>
<div class="field">
<%= f.label :year %><br>
<%= f.text_field :year %>
</div>
<div class="field">
<%= f.label :color %><br>
<%= f.text_field :color %>
</div>
<div class="field">
<%= f.label :serial %><br>
<%= f.text_field :serial %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :condition %><br>
<%= f.number_field :condition %>
</div>
<div class="field">
<%= f.label :kind %><br>
<%= f.text_field :kind %>
</div>
<div class="field">
<%= f.label :bodykind %><br>
<%= f.text_field :bodykind %>
</div>
<div class="field">
<%= f.label :frets %><br>
<%= f.text_field :frets %>
</div>
<div class="field">
<%= f.label :oneowner %><br>
<%= f.check_box :oneowner %>
</div>
<div class="field">
<%= f.label :extended_description %><br>
<%= f.text_area :description %>
</div>
<%= f.fields_for :photos do |builder| %>
<%= builder.label :photo, "Image File" %>
<%= builder.file_field :photo %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
db/schema.rb
ActiveRecord::Schema.define(version: 20131014195859) do
create_table "guitars", force: true do |t|
t.string "make"
t.string "model"
t.string "year"
t.string "color"
t.string "serial"
t.string "price"
t.integer "condition"
t.string "kind"
t.string "bodykind"
t.string "frets"
t.boolean "oneowner"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "description"
end
add_index "guitars", ["user_id"], name: "index_guitars_on_user_id", using: :btree
create_table "photos", force: true do |t|
t.integer "guitar_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "photo_file_name"
t.string "photo_content_type"
t.integer "photo_file_size"
t.datetime "photo_updated_at"
end
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "city"
t.string "state"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
解决方案
There was a typo.
:photos_attributes => [:photo]
这篇关于多个回形针文件上传Rails4上的不允许的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文