我有两个模型僵尸和推特。架构如下:
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在Zombie#show视图中,我添加了一个"New Tweet“按钮,将其发送到Tweet#new (new_tweet_path)。在Tweet#new视图中,我有一个包含两个字段的表单: status和zombie_id。当我进入来自僵尸个人资料的Tweet#new页面时,我不想填写zombie_id,或者我想让它知道id是什么,因为我只是来自上一页的个人资料。
我需要做些什么才能做到这一点?我假设我需要将僵尸对象从Zombie#show页面发送到Tweet#new页面,但是我不确定需要在控制器或视图中做什么来处理这个问题。有什么建议吗?
发布于 2012-04-23 13:25:34
在Zombie#show视图中,将zombie_id参数添加到new_tweet_path调用,如下所示:
new_tweet_path(zombie_id: @zombie.id)然后在Tweet#new中创建一个已经填充了zombie_id的Tweet模型,该模型在params hash中传递:
@tweet = Tweet.new(zombie_id: params[:zombie_id])https://stackoverflow.com/questions/10275068
复制相似问题