views:

46

answers:

2

I am just trying to print the parameters that have been entered into my form. Basically I create a new bet then I display the parameters:

MIGRATION

class CreateBets < ActiveRecord::Migration

def self.up
    create_table :bets do |t|
    t.integer :accepted ,:default => 0
    t.integer :user_1_id #proposer
    t.integer :user_2_id #receiver
    t.integer :team_1_id #proposer's team
    t.integer :team_2_id #receiver's team
    t.integer :game_id
    t.integer :winner
    t.integer :amount
    t.timestamps
   end
 end

  def self.down
    drop_table :bets
  end
end

CONTROLLER

bets_controller.erb

class BetsController < ApplicationController

  def index
    redirect_to new_bet_path
  end

  def new
    @b=Bet.new
  end

  def create
    @points=params[:points]
    @winner=params[:winner]
  end

end

VIEWS New.erb

<% facebook_form_for Bet.new do |f| %>
  <%= f.text_field :amount, :label=>"points" %>
  <%= f.text_field :winner, :label=>"WinningTeam" %>
  <%= f.buttons "Bet" %>
<% end %>

create.erb

points:<%= @points  %>
<br>
winner:<%= @winner  %>

I tried to make this code work with instance variables but it didn't work either. Where is the problem?
Thank you.

+1  A: 

I think that params[:winner] and params[:point] is empty hash. Try adding this to your create.erb:

params: <%= params.inspect %>

It will display your params hash, so you will see how to get to it.

Another hint, why you are creating new object in new action and then in form you are doing it again? So:

<% facebook_form_for @b   do |f| %>

And another thing, it is really good to keep naming conventions, so don't create @b object, but @bet.

In create action you should have line like this:

@bet = Bet.new(params[:bet])

And in view:

<p>
  points:<%= @bet.points %>
</p>
<p>
  winner:<%= @bet.winner %>
</p>

If you use <br> it's better to use <br/>.

Your index action is totaly useless. It would be better if you would move all behaviour from new action to index and remove new action completly.

klew
A: 

As klew pointed, for me it seems that you're getting empty params[:winner]and params[:point]. You can make sure that of what you're getting by taking a look at your servers log.

You will see a line like

Processing BetsController#create (for 127.0.0.1 at 2010-04-11 20:56:51) [POST]
Parameters: {"your"=>"parameters", "should"=>"apper in this hash"}
Francisco