未经许可的参数载波(Unpermitted Parameter carrierwave)
我正在尝试使用carrierwave gem在嵌套表单中上传文档。 当我提交表单时,我可以看到有一个未经许可的参数。
Started POST "/debts" for ::1 at 2016-05-18 11:10:27 +1000 Started POST "/debts" for ::1 at 2016-05-18 11:10:27 +1000 Processing by DebtsController#create as HTML Processing by DebtsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"fEmDQnog42ug97IllLL5A6g7oNueLaXIYwKpOEEqBwwjRdVkncNoPbeZuHsuD/HWz1PWGnD7m6lUyZmRJCKS9A==", "debt"=>{"amount"=>"", "details"=>"", "debtor_attributes"=>{"business_name"=>"", "abn"=>"", "first_name"=>"", "mobile_number"=>"", "last_name"=>"", "phone_number"=>"", "email"=>""}, "creditor_attributes"=>{"business_name"=>"", "abn"=>"", "first_name"=>"", "mobile_number"=>"", "last_name"=>"", "phone_number"=>"", "email"=>""}, "document"=>{"document"=>#<ActionDispatch::Http::UploadedFile:0x007f8fbbee2698 @tempfile=#<Tempfile:/var/folders/sn/t0vd19_x6mg1fkn8b4x1v8lm0000gn/T/RackMultipart20160518-27972-zmqd57.jpg>, @original_filename="pexels-photo (2).jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"debt[document][document]\"; filename=\"pexels-photo (2).jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Debt"} Parameters: {"utf8"=>"✓", "authenticity_token"=>"fEmDQnog42ug97IllLL5A6g7oNueLaXIYwKpOEEqBwwjRdVkncNoPbeZuHsuD/HWz1PWGnD7m6lUyZmRJCKS9A==", "debt"=>{"amount"=>"", "details"=>"", "debtor_attributes"=>{"business_name"=>"", "abn"=>"", "first_name"=>"", "mobile_number"=>"", "last_name"=>"", "phone_number"=>"", "email"=>""}, "creditor_attributes"=>{"business_name"=>"", "abn"=>"", "first_name"=>"", "mobile_number"=>"", "last_name"=>"", "phone_number"=>"", "email"=>""}, "document"=>{"document"=>#<ActionDispatch::Http::UploadedFile:0x007f8fbbee2698 @tempfile=#<Tempfile:/var/folders/sn/t0vd19_x6mg1fkn8b4x1v8lm0000gn/T/RackMultipart20160518-27972-zmqd57.jpg>, @original_filename="pexels-photo (2).jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"debt[document][document]\"; filename=\"pexels-photo (2).jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Debt"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] Unpermitted parameter: document Unpermitted parameter: document Role Load (0.1ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin?') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]] Role Load (0.1ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin?') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]] Unpermitted parameter: document Unpermitted parameter: document Unpermitted parameter: document Unpermitted parameter: document Unpermitted parameter: document Unpermitted parameter: document (0.1ms) BEGIN (0.1ms) BEGIN SQL (0.3ms) INSERT INTO "creditors" ("business_name", "first_name", "last_name", "email", "mobile_number", "phone_number", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["business_name", ""], ["first_name", ""], ["last_name", ""], ["email", ""], ["mobile_number", ""], ["phone_number", ""], ["user_id", 1], ["created_at", "2016-05-18 01:10:27.139053"], ["updated_at", "2016-05-18 01:10:27.139053"]] SQL (0.3ms) INSERT INTO "creditors" ("business_name", "first_name", "last_name", "email", "mobile_number", "phone_number", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["business_name", ""], ["first_name", ""], ["last_name", ""], ["email", ""], ["mobile_number", ""], ["phone_number", ""], ["user_id", 1], ["created_at", "2016-05-18 01:10:27.139053"], ["updated_at", "2016-05-18 01:10:27.139053"]] SQL (0.2ms) INSERT INTO "debtors" ("business_name", "first_name", "last_name", "email", "mobile_number", "phone_number", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["business_name", ""], ["first_name", ""], ["last_name", ""], ["email", ""], ["mobile_number", ""], ["phone_number", ""], ["user_id", 1], ["created_at", "2016-05-18 01:10:27.140363"], ["updated_at", "2016-05-18 01:10:27.140363"]] SQL (0.2ms) INSERT INTO "debtors" ("business_name", "first_name", "last_name", "email", "mobile_number", "phone_number", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["business_name", ""], ["first_name", ""], ["last_name", ""], ["email", ""], ["mobile_number", ""], ["phone_number", ""], ["user_id", 1], ["created_at", "2016-05-18 01:10:27.140363"], ["updated_at", "2016-05-18 01:10:27.140363"]] SQL (0.3ms) INSERT INTO "debts" ("details", "user_id", "status", "creditor_id", "debtor_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["details", ""], ["user_id", 1], ["status", "Pre Collection"], ["creditor_id", 28], ["debtor_id", 28], ["created_at", "2016-05-18 01:10:27.141270"], ["updated_at", "2016-05-18 01:10:27.141270"]] SQL (0.3ms) INSERT INTO "debts" ("details", "user_id", "status", "creditor_id", "debtor_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["details", ""], ["user_id", 1], ["status", "Pre Collection"], ["creditor_id", 28], ["debtor_id", 28], ["created_at", "2016-05-18 01:10:27.141270"], ["updated_at", "2016-05-18 01:10:27.141270"]] (2.0ms) COMMIT (2.0ms) COMMIT Redirected to http://localhost:3000/debts/28 Redirected to http://localhost:3000/debts/28 Completed 302 Found in 20ms (ActiveRecord: 3.1ms) Completed 302 Found in 20ms (ActiveRecord: 3.1ms)
我允许控制器中的参数
def debt_params params.require(:debt).permit(:user_id, :amount, :commission, :invoice_issued, :invoice_date, :status, :details, document_attributes: [:id, :debt_id, :document], debtor_attributes: [:id, :user_id, :business_name, :abn, :first_name, :last_name, :email, :mobile_number, :phone_number], creditor_attributes: [:id, :user_id, :business_name, :abn, :first_name, :last_name, :email, :mobile_number, :phone_number]) end
该模型接受嵌套属性
class Debt < ActiveRecord::Base belongs_to :user belongs_to :creditor belongs_to :debtor has_many :documents accepts_nested_attributes_for :debtor accepts_nested_attributes_for :creditor accepts_nested_attributes_for :documents end
不确定我在这里缺少什么?
I'm trying to upload a document within a nested form using the carrierwave gem. When I submit the form, I can see that there is an unpermitted parameter.
Started POST "/debts" for ::1 at 2016-05-18 11:10:27 +1000 Started POST "/debts" for ::1 at 2016-05-18 11:10:27 +1000 Processing by DebtsController#create as HTML Processing by DebtsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"fEmDQnog42ug97IllLL5A6g7oNueLaXIYwKpOEEqBwwjRdVkncNoPbeZuHsuD/HWz1PWGnD7m6lUyZmRJCKS9A==", "debt"=>{"amount"=>"", "details"=>"", "debtor_attributes"=>{"business_name"=>"", "abn"=>"", "first_name"=>"", "mobile_number"=>"", "last_name"=>"", "phone_number"=>"", "email"=>""}, "creditor_attributes"=>{"business_name"=>"", "abn"=>"", "first_name"=>"", "mobile_number"=>"", "last_name"=>"", "phone_number"=>"", "email"=>""}, "document"=>{"document"=>#<ActionDispatch::Http::UploadedFile:0x007f8fbbee2698 @tempfile=#<Tempfile:/var/folders/sn/t0vd19_x6mg1fkn8b4x1v8lm0000gn/T/RackMultipart20160518-27972-zmqd57.jpg>, @original_filename="pexels-photo (2).jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"debt[document][document]\"; filename=\"pexels-photo (2).jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Debt"} Parameters: {"utf8"=>"✓", "authenticity_token"=>"fEmDQnog42ug97IllLL5A6g7oNueLaXIYwKpOEEqBwwjRdVkncNoPbeZuHsuD/HWz1PWGnD7m6lUyZmRJCKS9A==", "debt"=>{"amount"=>"", "details"=>"", "debtor_attributes"=>{"business_name"=>"", "abn"=>"", "first_name"=>"", "mobile_number"=>"", "last_name"=>"", "phone_number"=>"", "email"=>""}, "creditor_attributes"=>{"business_name"=>"", "abn"=>"", "first_name"=>"", "mobile_number"=>"", "last_name"=>"", "phone_number"=>"", "email"=>""}, "document"=>{"document"=>#<ActionDispatch::Http::UploadedFile:0x007f8fbbee2698 @tempfile=#<Tempfile:/var/folders/sn/t0vd19_x6mg1fkn8b4x1v8lm0000gn/T/RackMultipart20160518-27972-zmqd57.jpg>, @original_filename="pexels-photo (2).jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"debt[document][document]\"; filename=\"pexels-photo (2).jpg\"\r\nContent-Type: image/jpeg\r\n">}}, "commit"=>"Create Debt"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] Unpermitted parameter: document Unpermitted parameter: document Role Load (0.1ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin?') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]] Role Load (0.1ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin?') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]] Unpermitted parameter: document Unpermitted parameter: document Unpermitted parameter: document Unpermitted parameter: document Unpermitted parameter: document Unpermitted parameter: document (0.1ms) BEGIN (0.1ms) BEGIN SQL (0.3ms) INSERT INTO "creditors" ("business_name", "first_name", "last_name", "email", "mobile_number", "phone_number", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["business_name", ""], ["first_name", ""], ["last_name", ""], ["email", ""], ["mobile_number", ""], ["phone_number", ""], ["user_id", 1], ["created_at", "2016-05-18 01:10:27.139053"], ["updated_at", "2016-05-18 01:10:27.139053"]] SQL (0.3ms) INSERT INTO "creditors" ("business_name", "first_name", "last_name", "email", "mobile_number", "phone_number", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["business_name", ""], ["first_name", ""], ["last_name", ""], ["email", ""], ["mobile_number", ""], ["phone_number", ""], ["user_id", 1], ["created_at", "2016-05-18 01:10:27.139053"], ["updated_at", "2016-05-18 01:10:27.139053"]] SQL (0.2ms) INSERT INTO "debtors" ("business_name", "first_name", "last_name", "email", "mobile_number", "phone_number", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["business_name", ""], ["first_name", ""], ["last_name", ""], ["email", ""], ["mobile_number", ""], ["phone_number", ""], ["user_id", 1], ["created_at", "2016-05-18 01:10:27.140363"], ["updated_at", "2016-05-18 01:10:27.140363"]] SQL (0.2ms) INSERT INTO "debtors" ("business_name", "first_name", "last_name", "email", "mobile_number", "phone_number", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["business_name", ""], ["first_name", ""], ["last_name", ""], ["email", ""], ["mobile_number", ""], ["phone_number", ""], ["user_id", 1], ["created_at", "2016-05-18 01:10:27.140363"], ["updated_at", "2016-05-18 01:10:27.140363"]] SQL (0.3ms) INSERT INTO "debts" ("details", "user_id", "status", "creditor_id", "debtor_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["details", ""], ["user_id", 1], ["status", "Pre Collection"], ["creditor_id", 28], ["debtor_id", 28], ["created_at", "2016-05-18 01:10:27.141270"], ["updated_at", "2016-05-18 01:10:27.141270"]] SQL (0.3ms) INSERT INTO "debts" ("details", "user_id", "status", "creditor_id", "debtor_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["details", ""], ["user_id", 1], ["status", "Pre Collection"], ["creditor_id", 28], ["debtor_id", 28], ["created_at", "2016-05-18 01:10:27.141270"], ["updated_at", "2016-05-18 01:10:27.141270"]] (2.0ms) COMMIT (2.0ms) COMMIT Redirected to http://localhost:3000/debts/28 Redirected to http://localhost:3000/debts/28 Completed 302 Found in 20ms (ActiveRecord: 3.1ms) Completed 302 Found in 20ms (ActiveRecord: 3.1ms)
I have permitted the params in the controller
def debt_params params.require(:debt).permit(:user_id, :amount, :commission, :invoice_issued, :invoice_date, :status, :details, document_attributes: [:id, :debt_id, :document], debtor_attributes: [:id, :user_id, :business_name, :abn, :first_name, :last_name, :email, :mobile_number, :phone_number], creditor_attributes: [:id, :user_id, :business_name, :abn, :first_name, :last_name, :email, :mobile_number, :phone_number]) end
The model accepts nested attributes for
class Debt < ActiveRecord::Base belongs_to :user belongs_to :creditor belongs_to :debtor has_many :documents accepts_nested_attributes_for :debtor accepts_nested_attributes_for :creditor accepts_nested_attributes_for :documents end
Not sure what I'm missing here?
原文:https://stackoverflow.com/questions/37288649
最满意答案
假设文件按顺序编号,绝对可以使用while循环:
i = 0 while True: i += 1 filename = 'arc.%d.txt' % i try: with open(filename, 'r') as file_handle: ... except IOError: break
虽然这对所有的筑巢都变得非常难看。 你最好使用
glob.glob
东西来获取文件名列表。from glob import glob filenames = glob('arc.*.txt') for filename in filenames: with open(filename) as file_handle: ...
有一些与第二种方法相关的竞争条件 - 如果文件以某种方式在
glob
找到它之间以及实际上是时候处理文件之间被删除那么你的程序可能会有糟糕的一天。It is definitely possible to use a while loop assuming that the files are numbered in sequential order:
i = 0 while True: i += 1 filename = 'arc.%d.txt' % i try: with open(filename, 'r') as file_handle: ... except IOError: break
Though this becomes pretty ugly with all the nesting. You're probably better off getting the list of filenames using something like
glob.glob
.from glob import glob filenames = glob('arc.*.txt') for filename in filenames: with open(filename) as file_handle: ...
There are some race conditions associated with this second approach -- If the file somehow gets deleted between when
glob
found it and when it actually is time to process the file then your program could have a bad day.
相关问答
更多-
假设文件按顺序编号,绝对可以使用while循环: i = 0 while True: i += 1 filename = 'arc.%d.txt' % i try: with open(filename, 'r') as file_handle: ... except IOError: break 虽然这对所有的筑巢都变得非常难看。 你最好使用glob.glob东西来获取文件名列表。 from glob import ...
-
你的代码非常错误。 首先,你不是为你的目录上的每个文本文件循环,而是只有一个文件。 看看这一行 File txt = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC) + "/" + filenamearray[0] + ".txt"); 它应该是 File txt = new File(getExternalFilesDir(Environment.DIRECTORY_MUSIC) + "/" + filenamearra ...
-
假设您列出的JSP的名称是ScoreFinder.jsp,并且您要将记录提交到同一个JSP。 您可以从参数id的值创建文本文件的名称,例如 <% String id = request.getParameter("id"); String fileName = "/"+id.replaceAll(" ", "")+".txt"; // avoiding null check of 'id' for simplicity Scanner sc = new Scan ...
-
用于循环遍历命名文件夹以读取每个.txt文件名和内容(For loop over named folder to read each .txt filename and contents)[2022-05-28]
这是你可以尝试的另一种方式。 DEFINE ('PAGES', './pages/'); //Define the directory path $directory = new DirectoryIterator(PAGES); //Get all the contents in the directory foreach ($directory as $files) { //Check that the contents of the directory are each files and the ... -
您只需要循环文件名一次,然后使用嵌套循环来获取文件中的每一行。 希望有所帮助! files = glob.glob('/Users/andreasportelli/Desktop/sqldata/*') customer = {"customer_id":[],"order_id":[]} #create a dictionary for file in files: customer["customer_id"].append(file) for ...
-
一个基本的例子: $Temp = Get-ChildItem C:\Temp -Force ForEach ($f in $Temp) { (Get-Content $f) | % { $_ -replace 'pop', 'bar' } | Set-Content $f } A basic example: $Temp = Get-ChildItem C:\Temp -Force ForEach ($f in $Temp) { (Get-Content $f) ...
-
在我看来,你正试图用'two'扩展来编写你的文件,因为你像filenameWithExtension一样读它们,而不是你把它们写成filenameWithExtension.txt。 如果是这种情况,解决方案只是将此paste(file.names[i],".txt",sep="")更改为此file.names[i] 。 如果我错了,你应该向我们展示file.name的内容的一个例子。 It seems to me that you are trying to write your files with ' ...
-
R循环打开文件(R loop opening files)[2019-12-19]
由于Paul Hiemstra的答案回答#1,这里是#2的答案,假设“答案”是指“ abouheif.moran(pruned.tree.ja.chem.w.in) ”的印刷输出。 使用带有参数append = true cat() 。 例如: output_file = "my_output_file.txt" for(i in files) { # do stuff # make plots out <- abouheif.moran(pruned.tree.ja.chem.w. ... -
批处理 - 比较两个txt文件(Batch - Comparing two txt files)[2022-11-10]
下面的代码保留第一行中的File2.txt与File1.txt不同的行: @echo off setlocal EnableDelayedExpansion rem Redirect the *larger* file as input < File2.txt ( rem Merge it with lines from shorter file for /F "delims=" %%a in (file1.txt) do ( rem Read the next line fr ... -
@echo on cls cd /d "Z:\" for /F "tokens=*" %%A in (C:\Users\user1\Desktop\network\folder_list.txt) do ( pushd %%A echo %%A dir /S/Q *.htm,*.html,*.aspx,*.asp,*.php >> C:\Users\user1\Desktop\network\files_search_output.csv popd ) echo "cd\" pause 第一个问题 - 在 ...