Max
Max

Reputation: 45

I can't insert to my database rails

i can't insert to my database whats is my problem?

it's bowling game and i have two tables with name "Player" and "Result"

view

<%= form_for player_new_path(@player) do |f|%>
<div class="text_field">
    <p>
    <%= f.label "Spelare namn" %>
    <%= f.text_field :name %>
    </p>
    <p> 
    <%= f.submit "Lägg till en spelare"%>
    </p>
</div>
Controller
 def create
   @player = Player.new(params[:players])

  if @player.save
    redirect_to players_new_path
   else
  render :action => "new"
end        

end

Not work :/ my model:

class Player < ActiveRecord::Base # attr_accessible :title, :body 
    belongs_to :result 
end 

and my migrations:

class CreatePlayers < ActiveRecord::Migration
    def change 
        create_table :players do |t|
        t.string "name"
        t.references :results 
        t.timestamps
end

Upvotes: 0

Views: 159

Answers (2)

DVG
DVG

Reputation: 17470

@player = Player.new(params[:players]) should probably be @player = Player.new(params[:player]) (You are getting a single player as a param)

Otherwise, what error are you getting

Upvotes: 0

dogenpunk
dogenpunk

Reputation: 4382

Check your params hash. I bet the key isn't 'players', it's probably 'player'.

Upvotes: 3

Related Questions