Hello all, I am pulling my hair out on this. It seems like it should be the easiest thing in the world to do, but it''s just escaping me. I have a rails built form and but I keep getting this error when I submit it: "Mysql::Error: Column ''user_id'' cannot be null: INSERT INTO `games` (..." I have a hidden field in my form that collects @user.id which is passed by the controller. <% form_for @game, :url => user_games_path(@user), :method => :post do |f| %> <%= f.hidden_field :user_id, :value => @user.id %> <div class="formfield"> <%= f.label :my_user_empire %> <%= f.select :user_empire, Game::SelectableEmpires.options_for_select %> </div> <div class="formfield"> <%=f.submit "Begin Game" %> </div> <% end %> Controller: def new @user = User.find(params[:user_id]) @game = Game.new end And I am pretty sure the problem is b/c the HTML hidden field is sending the database a string and the DB is expecting an integer. I''ve tried this in the model: before_validation :user_id def user_id self.user_id.to_i end but I get the error: "stack level too deep" I would greatly appreciate any help. Thank you. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Srikanth Shreenivas
2010-Sep-12 16:21 UTC
Re: html input strings to database column integers
In your controller, may be you need to set the user object in games object. def new @user = User.find(params[:user_id]) @game = Game.new @game.user = @user end In your current code, there is nothing in the @game that links it to @user. On Sun, Sep 12, 2010 at 9:39 PM, Keith Raymond <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello all, > > I am pulling my hair out on this. It seems like it should be the easiest > thing in the world to do, but it''s just escaping me. > > I have a rails built form and but I keep getting this error when I > submit it: > > "Mysql::Error: Column ''user_id'' cannot be null: INSERT INTO `games` > (..." > > I have a hidden field in my form that collects @user.id which is passed > by the controller. > > <% form_for @game, :url => user_games_path(@user), :method => :post do > |f| %> > <%= f.hidden_field :user_id, :value => @user.id %> > <div class="formfield"> > <%= f.label :my_user_empire %> > <%= f.select :user_empire, > Game::SelectableEmpires.options_for_select %> > </div> > > <div class="formfield"> > <%=f.submit "Begin Game" %> > </div> > <% end %> > > Controller: > def new > @user = User.find(params[:user_id]) > @game = Game.new > end > > And I am pretty sure the problem is b/c the HTML hidden field is sending > the database a string and the DB is expecting an integer. > > I''ve tried this in the model: > > before_validation :user_id > > def user_id > self.user_id.to_i > end > > but I get the error: > "stack level too deep" > > I would greatly appreciate any help. Thank you. > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Srikanth Shreenivas
2010-Sep-12 16:24 UTC
Re: html input strings to database column integers
If below hint does not solve your problem, please paste your Game model code (apps/model/game.rb) here. I hope you have "belongs_to" or "has_one" relationship setup between game and user. Regards, Srikanth On Sun, Sep 12, 2010 at 9:51 PM, Srikanth Shreenivas <srikps-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> In your controller, may be you need to set the user object in games object. > > > def new > @user = User.find(params[:user_id]) > @game = Game.new > > @game.user = @user > > end > > > In your current code, there is nothing in the @game that links it to @user. > > > > On Sun, Sep 12, 2010 at 9:39 PM, Keith Raymond <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote: > >> Hello all, >> >> I am pulling my hair out on this. It seems like it should be the easiest >> thing in the world to do, but it''s just escaping me. >> >> I have a rails built form and but I keep getting this error when I >> submit it: >> >> "Mysql::Error: Column ''user_id'' cannot be null: INSERT INTO `games` >> (..." >> >> I have a hidden field in my form that collects @user.id which is passed >> by the controller. >> >> <% form_for @game, :url => user_games_path(@user), :method => :post do >> |f| %> >> <%= f.hidden_field :user_id, :value => @user.id %> >> <div class="formfield"> >> <%= f.label :my_user_empire %> >> <%= f.select :user_empire, >> Game::SelectableEmpires.options_for_select %> >> </div> >> >> <div class="formfield"> >> <%=f.submit "Begin Game" %> >> </div> >> <% end %> >> >> Controller: >> def new >> @user = User.find(params[:user_id]) >> @game = Game.new >> end >> >> And I am pretty sure the problem is b/c the HTML hidden field is sending >> the database a string and the DB is expecting an integer. >> >> I''ve tried this in the model: >> >> before_validation :user_id >> >> def user_id >> self.user_id.to_i >> end >> >> but I get the error: >> "stack level too deep" >> >> I would greatly appreciate any help. Thank you. >> -- >> Posted via http://www.ruby-forum.com/. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Here is my games model. I do have a belongs_to relationship with users and users has_one game. The problem isn''t with this relationship though. I am able to get retrieve any information related to the @user, no problem. The problem I am having is that in my form I have html inputs that need to post to my database. But the database is looking for integers here and the form outputs strings. I need to know if there''s an easy (or any) to have these values from my form be converted to integers before_save. I thought that what I have below with defining "user_id" as "self.user_id.to_i" would do it but I get that stack error. class Game < ActiveRecord::Base belongs_to :user before_validation :user_id before_validation :user_empire def user_id self.user_id.to_i end def user_empire self.user_empire.to_i end end Again my form: <% form_for @game, :url => user_games_path(@user), :method => :post do |f| %> <%= f.hidden_field :user_id, :value => @user.id %> <div class="formfield"> <%= f.label :my_user_empire %> <%= f.select :user_empire, Game::SelectableEmpires.options_for_select %> </div> <div class="formfield"> <%=f.submit "Begin Game" %> </div> <% end %> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Srikanth Shreenivas
2010-Sep-12 17:03 UTC
Re: Re: html input strings to database column integers
Refer Section 7 here http://guides.rubyonrails.org/getting_started.html It gives an example similar to the problem you are trying to solve. Don''t think you need to define your own method "user_id", as active record would have already generated one based on DB schema. Also, conversion between integer and string is too taken care by Active Record. From a web form, all the data are received in string form in the HTTP POST body, so I dont think the issue is with data type conversion. Regards, Srikanth On Sun, Sep 12, 2010 at 10:20 PM, Keith Raymond <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Here is my games model. > > I do have a belongs_to relationship with users and users has_one game. > > The problem isn''t with this relationship though. I am able to get > retrieve any information related to the @user, no problem. The problem > I am having is that in my form I have html inputs that need to post to > my database. But the database is looking for integers here and the form > outputs strings. > > I need to know if there''s an easy (or any) to have these values from my > form be converted to integers before_save. I thought that what I have > below with defining "user_id" as "self.user_id.to_i" would do it but I > get that stack error. > > class Game < ActiveRecord::Base > > belongs_to :user > > before_validation :user_id > before_validation :user_empire > > def user_id > self.user_id.to_i > end > > def user_empire > self.user_empire.to_i > end > end > > Again my form: > <% form_for @game, :url => user_games_path(@user), :method => :post do > |f| %> > <%= f.hidden_field :user_id, :value => @user.id %> > <div class="formfield"> > <%= f.label :my_user_empire %> > <%= f.select :user_empire, > Game::SelectableEmpires.options_for_select %> > </div> > > <div class="formfield"> > <%=f.submit "Begin Game" %> > </div> > <% end %> > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thank you for your response. So I''ve realized that the model is just not receiving anything from these fields. I added: validates_presence_of :user_id validates_presence_of :user_empire validates_presence_of :difficulty and I get the error in the form: "User can''t be blank User empire can''t be blank" In the development log it shows these being posted with the correct names, I''ve double checked them in the DB and it all matches up. So why is it not submitting what''s in the input to the DB? Also it says "User can''t be Blank" when it should be user_id. I am completely confused here. I do have the belongs_to relationship with :users but am not nesting the new game form. I don''t have to still do the accepts_nested_attributes, do I? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Srikanth Shreenivas
2010-Sep-12 18:17 UTC
Re: Re: html input strings to database column integers
Can you please provide code of your Game controller? Also, let me know whether you are getting the error during create or update of game or both? On Sun, Sep 12, 2010 at 11:44 PM, Keith Raymond <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Thank you for your response. > > So I''ve realized that the model is just not receiving anything from > these fields. > > I added: > validates_presence_of :user_id > validates_presence_of :user_empire > validates_presence_of :difficulty > > and I get the error in the form: > "User can''t be blank > User empire can''t be blank" > > In the development log it shows these being posted with the correct > names, I''ve double checked them in the DB and it all matches up. So why > is it not submitting what''s in the input to the DB? > > Also it says "User can''t be Blank" when it should be user_id. I am > completely confused here. > > I do have the belongs_to relationship with :users but am not nesting the > new game form. I don''t have to still do the accepts_nested_attributes, > do I? > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Keith Raymond
2010-Sep-12 18:29 UTC
Re: Re: html input strings to database column integers
Here is my games_controller: class GamesController < ApplicationController def show @user = User.find(params[:user_id]) @game = Game.find(params[:id]) end def new @user = User.find(params[:user_id]) @game = Game.new end def create @user = User.find(params[:user_id]) @game = Game.new(params[:id]) if @game.save redirect_to user_game_path(@user, @game) else render :action => ''new'' end end def edit end def update end def destroy end end As you can see, I haven''t even begun to fight with update or edit. Updates to the games table will occur later with responses from a flash document. From my dev_log. It shows that the fields I want are getting posted. But Rails is not seeing them. Processing UsersController#1 (for 127.0.0.1 at 2010-09-12 14:23:26) [PUT] Parameters: {"user"=>{"game"=>{"num_of_enemies"=>"7", "enemy_check_box"=>["0", "50", "0", "0", "30", "0", "0", "20", "0", "0", "0"], "user_id"=>"1", "user_empire"=>"30", "difficulty"=>"10", "selection_of_enemies"=>"2"}}, "commit"=>"Begin Game", "action"=>"1", "_method"=>"put", "authenticity_token"=>"SX7VfUzPm3SdzqFoKX4DQObQaHQLXSJudlNIfEGUt2Q=", "id"=>"games", "controller"=>"users"} -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Srikanth Shreenivas
2010-Sep-12 18:46 UTC
Re: Re: Re: html input strings to database column integers
In the "create" method, there is nothing that links "user" and "game" together...that is causing the issue. I am little rusty on exact syntax, but based on my rails code I could refer, I suggest trying out followings: Based on your logs, user_id is nested within user hash, so i suggest you use params[:user][:user_id] in User.find. Also, use the in-built create method to create game instance from user instance (Refer http://guides.rubyonrails.org/association_basics.html#belongs_to-association-reference ). def create @user = User.find(params*[:user]*[:user_id]) @game = @user*.create_game(params[:game]) * if @game.save redirect_to user_game_path(@user, @game) else render :action => ''new'' end end On Sun, Sep 12, 2010 at 11:59 PM, Keith Raymond <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Here is my games_controller: > > class GamesController < ApplicationController > def show > @user = User.find(params[:user_id]) > @game = Game.find(params[:id]) > end > > def new > @user = User.find(params[:user_id]) > @game = Game.new > end > > def create > @user = User.find(params[:user_id]) > @game = Game.new(params[:id]) > > if @game.save > redirect_to user_game_path(@user, @game) > else > render :action => ''new'' > end > end > > def edit > > end > > def update > > end > > def destroy > > end > end > > As you can see, I haven''t even begun to fight with update or edit. > Updates to the games table will occur later with responses from a flash > document. > > From my dev_log. It shows that the fields I want are getting posted. > But Rails is not seeing them. > > Processing UsersController#1 (for 127.0.0.1 at 2010-09-12 14:23:26) > [PUT] > Parameters: {"user"=>{"game"=>{"num_of_enemies"=>"7", > "enemy_check_box"=>["0", "50", "0", "0", "30", "0", "0", "20", "0", "0", > "0"], "user_id"=>"1", "user_empire"=>"30", "difficulty"=>"10", > "selection_of_enemies"=>"2"}}, "commit"=>"Begin Game", "action"=>"1", > "_method"=>"put", > "authenticity_token"=>"SX7VfUzPm3SdzqFoKX4DQObQaHQLXSJudlNIfEGUt2Q=", > "id"=>"games", "controller"=>"users"} > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Srikanth Shreenivas
2010-Sep-12 18:56 UTC
Re: Re: Re: html input strings to database column integers
I am sorry....user_id is nested with game hash....so use params[:game][:user_id] def create @user = User.find(params[:game][:user_id]) @game = @user.create_game(params[:game]) if @game.save redirect_to user_game_path(@user, @game) else render :action => ''new'' end end On Mon, Sep 13, 2010 at 12:16 AM, Srikanth Shreenivas <srikps-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> In the "create" method, there is nothing that links "user" and "game" > together...that is causing the issue. > > I am little rusty on exact syntax, but based on my rails code I could > refer, I suggest trying out followings: > > Based on your logs, user_id is nested within user hash, so i suggest you > use params[:user][:user_id] in User.find. > Also, use the in-built create method to create game instance from user > instance (Refer > http://guides.rubyonrails.org/association_basics.html#belongs_to-association-reference > ). > > def create > > @user = User.find(params*[:user]*[:user_id]) > @game = @user*.create_game(params[:game]) > * > if @game.save > redirect_to user_game_path(@user, @game) > else > render :action => ''new'' > end > end > > > > > On Sun, Sep 12, 2010 at 11:59 PM, Keith Raymond <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote: > >> Here is my games_controller: >> >> class GamesController < ApplicationController >> def show >> @user = User.find(params[:user_id]) >> @game = Game.find(params[:id]) >> end >> >> def new >> @user = User.find(params[:user_id]) >> @game = Game.new >> end >> >> def create >> @user = User.find(params[:user_id]) >> @game = Game.new(params[:id]) >> >> if @game.save >> redirect_to user_game_path(@user, @game) >> else >> render :action => ''new'' >> end >> end >> >> def edit >> >> end >> >> def update >> >> end >> >> def destroy >> >> end >> end >> >> As you can see, I haven''t even begun to fight with update or edit. >> Updates to the games table will occur later with responses from a flash >> document. >> >> From my dev_log. It shows that the fields I want are getting posted. >> But Rails is not seeing them. >> >> Processing UsersController#1 (for 127.0.0.1 at 2010-09-12 14:23:26) >> [PUT] >> Parameters: {"user"=>{"game"=>{"num_of_enemies"=>"7", >> "enemy_check_box"=>["0", "50", "0", "0", "30", "0", "0", "20", "0", "0", >> "0"], "user_id"=>"1", "user_empire"=>"30", "difficulty"=>"10", >> "selection_of_enemies"=>"2"}}, "commit"=>"Begin Game", "action"=>"1", >> "_method"=>"put", >> "authenticity_token"=>"SX7VfUzPm3SdzqFoKX4DQObQaHQLXSJudlNIfEGUt2Q=", >> "id"=>"games", "controller"=>"users"} >> -- >> Posted via http://www.ruby-forum.com/. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.