2014-05-03 83 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 –