2013-08-27 29 views
-2

美好的一天。 我是新來的紅寶石,我試圖建立一個嵌套的「鏈接」字段的簡單發佈表單。RoR尋找專欄我已經使用遷移命令刪除了專欄

如:

***** 
TITLE 
***** 
POST 
**** 

LINK - 1 
LINK - 2 
. 
. 
LINK - N 
**** 

使用3個遷移文件我有以下模式文件

ActiveRecord::Schema.define(version: 20130827060014) do 

    create_table "links", force: true do |t| 
    t.text  "link" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    create_table "posts", force: true do |t| 
    t.string "title" 
    t.text  "body" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

end 

第2遷移文件是創建表,當我創建的鏈接表我還添加了post_id列。 現在我已經決定使用下面的命令來刪除此列:

$->rails g migration remove_field_name_from_links post_id:links 
$->rake db:migrant 

的,儘管該列並不「新」控制器後看起來像這樣

def new 
    @new_post = Post.new 
    3.times {@new_post.links.build} 
end 

的問題是存在了它出現在系統仍在尋找它,因爲我得到了以下錯誤消息:

SQLite3::SQLException: no such column: links.post_id: SELECT "links".* FROM "links" WHERE "links"."post_id" = ? 

這是視圖文件:

<h1> <%= @post.title %></h1> 
<p> <%= @post.body %></P> 
<ul> 
    <%= for links in @post.links do %> 
     <li> 
      <%= links.link %> 
     </li> 
    <% end %> 
</ul> 

有什麼想法嗎?

+0

你能告訴你鏈接控制器...? – Sami

+0

鏈接嵌入帖子內,我沒有單獨的控制器。儘管我已經對後期模型進行了以下更改。 \t has_many:links,:dependent =>:destroy accepted_nested_attributes_for:links – shultz

+0

檢查您是否已將「removed_field」從鏈接模型中的attr_accessible中移除。 – Sami

回答

0

刪除ul的所有內容標籤列出您的文章的鏈接可能會解決您的錯誤。既然你不想在links表中列post_id列,我猜你也不想要這個關聯。因此,當您完成刪除操作後,請從模型中刪除此關聯,並從控制器中刪除正確的呼叫(例如3.times {@new_post.links.build})。

發生錯誤是因爲當您有關聯時,Rails預計links表具有post_id列。由於您刪除了此列,因此會出現錯誤。