ffmike (owner)

Revisions

gist: 200275 Download_button fork
public
Public Clone URL: git://gist.github.com/200275.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Two ways to do this...pretty much equivalent on effort, so pick the one that reads
# more smoothly to you
 
# Alternative #1
 def create
    @topic = Topic.new(params[:topic])
# next line was your original problem, I think - you're not passing an id param, you're
# passing a game_id nested inside of topic
    @topic.game_id = params[:topic][:game_id]
    if @topic.save
      flash[:notice] = "Successfully created topic."
      redirect_to ([@game, @topic])
    else
      render :action => 'new'
    end
  end
 
# Alternative #2
  def create
    @game = Game.find(params[:topic][:game_id])
# switched to using build here - create saves, build doesn't
    @topic = @game.topics.build(params[:topic])
    if @topic.save
      flash[:notice] = "Successfully created topic."
      redirect_to ([@game, @topic])
    else
      render :action => 'new'
    end
  end