2012-01-11 46 views
1

這裏有幾個問題,coverthisalready,我知道。我是編程和導軌的新手,所以請耐心等待。我的目標是收集n標記對象並將它們顯示在我的顯示和索引操作中。質量分配屬性以嵌套形式失敗。無聲錯誤?

UPDATE 感謝所有回答的傢伙。每個建議都促使我朝着正確的方向發展。我可以通過傳入一個空數組來初始化tags對象來獲取rake任務來創建帖子。但是標籤仍然沒有創建。在進一步的檢查,我得到以下SQL異常:

irb(main):002:0> u.posts.build(title: "a new day", tags: "jump") 
WARNING: Can't mass-assign protected attributes: tags 
(1.7ms) SELECT 1 FROM "posts" WHERE "posts"."title" = 'a new day' LIMIT 1 
(0.5ms) COMMIT 
=> #<Post id: nil, title: "a new day", description: nil, content: nil, user_id: 1, created_at: nil, updated_at: nil> 

我的設立如下:

Tag型號

class Tag < ActiveRecord::Base 

belongs_to :post 

end 

Post型號

class Post < ActiveRecord::Base 

has_many :tags, autosave: true 
attr_accessible :title, :description, :content, :tags_attributes 
accepts_nested_attributes_for :tags, allow_destroy: true, reject_if: lambda {|attrs| attrs.all? {|key, value| value.blank?}} 
#add n number of form fields to capture tags in each article. 
    def with_blank_tags(n = 3) 
    n.times do 
     tags.build 
    end 
    self 
    end 
end 

'查看' 代碼

<%= form_for(@post.with_blank_tags) do |f| %> 
<div class="field"> 
    <%= f.fields_for(:tags) do |tags| %> 
    <%= unless tags.object.new_record? tags.check_box('_destroy') + tags.label('_destroy', 'Remove Tag') end%> 
    <%= tags.label :tags, "Add a Tag"%> 
    <%= tags.text_field :tags %> 
    <%end%> 
</div> 
<%end%> 

「控制」的代碼

def new 
@post = @user.posts.build 
end 

def create 
@post = @user.posts.build(params[:post]) 
    if @post.save? 
    respond_to do |format| 
    format.html { redirect_to @post, notice: 'Post was successfully created.' } 
    else 
    format.html { render action: :new } 
    end 
    end 
end 

我的耙子任務:

​​3210
+0

如果這件事情我會很驚訝。但是到目前爲止,web上看到的很多其他問題和示例都有在attr_accessible調用之前調用了接受嵌套屬性。可能需要在將#{name} _attributes白名單列出之前進行定義? – agmcleod 2012-01-11 18:58:55

+0

@agmcleod我按照您建議的順序發出了呼叫,並收到相同的異常。 – rhodee 2012-01-11 19:06:15

+0

@agmcleod我在這個[post](http://stackoverflow.com/questions/2792622/what-does-this-rake-dbseed-error-mean)中遇到了一個解決方案,建議重新命名模型。我想知道模型的名稱標籤是不可接受的嗎? – rhodee 2012-01-11 19:10:34

回答

0

我能解決這個問題。在rdocs here之後,爲了設置您的嵌套屬性,您將一個哈希數組傳遞給* _attributes。這消除了我上面描述的錯誤並設置了對象ID tags_id

我所要做的就是刪除此行:

tag = [] 
post = user.posts.create!(title: title, description: description, content: content, tags_attributes: tag) 

以及與此我rake任務裏面替換:

tag = Faker::Lorem.words(1) # create a tag 
post = user.posts.create!(tags_attributes: [tags: tags]) 

現在,當我這樣做Tag.all從我的控制檯我見:

[#<Tag id: 1, post_id: 1, tags: "---\n- adipisci\n", created_at: "2012-01-12 06:31:13", updated_at: "2012-01-12 06:31:13">, 
1

如果您在鐵軌模型聲明一些屬性爲attr_accessible,那麼所有其他屬性被自動設置爲attr_protected。在我看來,您的問題可能源於您嘗試創建帖子並同時指定tags屬性的事實。嘗試將:tags添加到Post模型中的attr_accessible屬性列表中,並查看是否修復了該屬性。

+0

謝謝@Batkins。我做了更改,並在控制檯中收到以下錯誤:rake中止了! Tag(#38643420)expected,got String(#18449760)' – rhodee 2012-01-11 19:04:04

+0

我不確定您是否可以通過用戶關係構建帖子,並通過帖子關係在一行中創建標籤。如'@post = @ user.posts.build(tags:Tag.new)'可能需要分成'@post = @ user.posts.build',然後用'@post.tags .build'。這只是一個猜測。 – Batkins 2012-01-11 19:17:32

+0

無論如何,它看起來像我的答案解決了問題,你在哪裏得到'不能大規模分配受保護的屬性問題,這是你問這個問題時你遇到的問題,所以你會接受我的答案是正確的?您可以將新錯誤作爲單獨的問題提出。 – Batkins 2012-01-11 21:02:06

0

在控制器的#create中,是不是要撥打@post.save?你也不需要第二個.tags方法。只是一個簡單的:

def create 
    @post = @user.posts.build(params[:post]) 
    if @post.save 
    redirect_to @post, notice: 'Post was successfully created.' } 
    else 
    render action: :new 
    end 
end 
+0

這在我的代碼庫中是正確的,但不是在這篇文章中。發生相同的錯誤。 – rhodee 2012-01-11 20:44:17

+0

遵循您的建議並從attr_accessible調用中刪除'tags'可以生成示例數據。謝謝。 – rhodee 2012-01-11 21:00:40

+0

檢查我的控制檯中的'post'對象後,看起來標記對象沒有保存。有任何想法嗎? – rhodee 2012-01-11 21:04:23