2014-05-03 81 views
0

我知道这里有很多这样的帖子,我认为我已经阅读并尝试了其中的每一个,都没有成功。rails 4不允许的参数嵌套形式

我有Post和Image模型,我需要与多对一的关系一起工作。

class Post < ActiveRecord::Base 
    has_many :images 
end 

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

这是在我的职位控制器,其中包括我的图像模型迁移所有字段的post_parms声明。

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

这里是我的后期创建表单,其中,我希望允许多个图像资产创建与每个职位。

<%= 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> 

尽管一再努力和阅读每一个岗位,我可以找到关于这个话题,我仍然得到:

Unpermitted parameters: image 

这里的问题是,这个错误没有提供线索从哪里开始寻找为了这个问题。因为我不确定下一步该去哪里,所以我想我会在这里发布,寻找更多的专业意见。

+0

问题解决了吗?如果没有,你可以分享PostController的内容吗? – HackerKarma

回答

2

更新Post模型如下:

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

在表单提交这样,您将获得的图像中关键images_attributes而不是image您目前正在接受这是造成的警告,Unpermitted parameters: image

属性

因为您有1-M relationshipPostImage

您需要更新post_params如下:

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

使用images_attributes通知的多个图像)在您的视图,而不是image_attributes通知奇异图像

,并更改fields_for作为

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

使用images注意复数)和NOT image通知奇异

UPDATE

要解决uninitialized constant Post::Image错误

更新Image模型如下:

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

此外,建议从

删除
<%= ff.file_field :file, multiple: true %> 
+0

感谢@ Kirti的帮助,在修复所有这些错误之后,当我尝试加载我的表单时,我现在变得“未初始化的常量Post :: Image”,有趣的是,当我将“post.fields_for:images”更改回复数“:形象”没有错误。我不确定我哪里出错了。 – greyoxide

+0

让我们在聊天上进行调试http://chat.stackoverflow.com/rooms/48530/ror –