简体   繁体   English

rails 4 unpermitted params嵌套表单

[英]rails 4 unpermitted params nested form

I know there are alot of these kinds of posts on SO, and I think Ive read and tried each of them, all without success. 我知道在SO上有很多这样的帖子,我想我已经阅读并尝试过各自的帖子,但都没有成功。

I have Post and Image models that I need to work together with a many to one relationship. 我有Post和Image模型,我需要与多对一的关系一起工作。

class Post < ActiveRecord::Base
  has_many :images
end

class Image < ActiveRecord::Base
  belongs_to :post
  mount_uploader :file, images_uploader
end

Here is the post_parms declaration in my posts controller, which includes all of the fields in my image model migration. 这是我的posts控制器中的post_parms声明,其中包含我的图像模型迁移中的所有字段。

private
 def post_params
  params.require(:post).permit(:title, :content, image_attributes: [:id, :post_id, :file])
end

and here is my post creation form where, I hope to allow multiple image asset creation with each post. 这是我的帖子创建表单,我希望允许每个帖子创建多个图像资源。

<%= form_for @post, html: {class: "pure-form pure-form-stacked"} do |post| %>

<%= post.fields_for :image, :html => { :multipart => true } do |image| %>
    <%= image.label :file, "Upload Image:" %>
    <%= image.file_field :file, multiple: true %>   
<% end %>

<fieldset class="post-form">
    <%= post.label :title %>
    <%= post.text_field :title %>

    <%= post.label :content %>
    <%= post.text_area :content, :class => "redactor", :rows => 40, :cols => 120 %>
</fieldset>

<div class = "button-box">
    <%= post.submit class: "pure-button pure-button-primary" %>
    <%= link_to "Cancel", posts_path, :class => "pure-button" %>
</div>

Despite repeated effort and reading every post I can find on this topic, I'm still getting: 尽管我一再努力并阅读我在这个主题上可以找到的每一篇文章,但我仍然得到:

Unpermitted parameters: image

The trouble here is that this error provides no clue where to start looking for the problem. 这里的麻烦是这个错误没有提供从哪里开始寻找问题的线索。 Since Im not really sure where to look next I thought I would post it here looking for more professional opinions. 由于我不确定下一步在哪里看,我想我会在这里发布,寻找更专业的意见。

Update the Post model as below: 更新Post模型如下:

class Post < ActiveRecord::Base
  has_many :images
  accepts_nested_attributes_for :images ## Add this
end

This way upon form submission you would receive the image attributes in key images_attributes and not image which you are currently receiving which is causing the warning as Unpermitted parameters: image 这样在表单提交时,您将收到关键images_attributes的图像属性,而不是您当前正在接收的image ,这会导致警告为未Unpermitted parameters: image

As you have 1-M relationship between Post and Image 因为PostImage之间有1-M relationship

You need to update post_params as below: 您需要更新post_params ,如下所示:

def post_params
  params.require(:post).permit(:title, :content, images_attributes: [:id, :post_id, :file])
end

Use images_attributes ( Notice plural images ) and NOT image_attributes ( Notice singular image ) 使用images_attributes注意多个图像 )和NOT image_attributes注意奇异图像

And change fields_for in your view as 并在视图中更改fields_for

<%= post.fields_for :images, :html => { :multipart => true } do |image| %>

Use images ( Notice plural ) and NOT image ( Notice singular ) 使用images注意复数 )和NOT image注意单数

UPDATE UPDATE

To resolve uninitialized constant Post::Image error 解决uninitialized constant Post::Image错误

Update Image model as below: 更新Image模型如下:

class Image < ActiveRecord::Base
  belongs_to :post
  ## Updated mount_uploader
  mount_uploader :file, ImagesUploader, :mount_on => :file
end

Also, suggested to remove multiple: :true from 另外,建议删除multiple: :true

<%= ff.file_field :file, multiple: true %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM