2012-04-23 54 views
0

我有兩個模型,殭屍和鳴叫。下面的模式:發送zombie_id來自殭屍#show to Tweet#新(一對多關係)

create_table "tweets", :force => true do |t| 
    t.string "status" 
    t.integer "zombie_id" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

    create_table "zombies", :force => true do |t| 
    t.string "name" 
    t.string "graveyard" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

用下面的關聯:

class Zombie < ActiveRecord::Base 
    has_many :tweets 
end 

class Tweet < ActiveRecord::Base 
    belongs_to :zombie 
end 

在殭屍#秀來看,我添加了一個「新建推」按鈕發送它鳴叫#新(new_tweet_path)。在Tweet#新視圖中,我有一個包含兩個字段的表單:status和zombie_id。當我到達來自Zombie個人資料的Tweet#新頁面時,我不想填寫zombie_id,或者我希望它知道該ID是什麼,因爲我剛剛從它的配置文件頁。

我需要做些什麼才能做到這一點?我假設我需要將Zombie#show頁面中的殭屍對象發送到Tweet#新頁面,但我不確定在控制器或視圖中需要做什麼來處理此問題。有什麼建議?

回答

1

在殭屍#顯示視圖添加zombie_id參數去的new_tweet_path調用是這樣的:

new_tweet_path(zombie_id: @zombie.id) 

然後在鳴叫#新創建已填寫zombie_id鳴叫模式,即在PARAMS哈希通過:

@tweet = Tweet.new(zombie_id: params[:zombie_id])