嵌套资源表单路径

时间:2012-03-14 18:55:30

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2

这是我的表格

<%= form_for([@game,@message_template]) do |f| %>

在我的控制器中我这样做:

def edit
  @message_template = MessageTemplate.find(params[:id])
  @game = Game.where(:slug => params[:game_id])
end

我的路线就像这样/games/:game_id/message_templates/:id

但我得到了这个

  

的未定义方法`model_name'

1 个答案:

答案 0 :(得分:0)

尝试设置arel搜索以返回第一个对象(对于@game)。

def edit
  @message_template = MessageTemplate.find(params[:id])
  @game = Game.where(:slug => params[:game_id]).first
end

routes.rb中:

#based on your comment, this will give you all (and likely more) of the paths you will need.
resources :games do
  resources :message_templates
end