I''m new to Ruby on Rails. I have two tables defined as
Table 1 - Leagues
id primary key - autonum
txtLeagueName
Table 2 - Divisions
id primary key - autonum
league_id
txtDivisionName
I can add a League fine but when I try and add a division I get the
error:
"Couldn''t find League without an ID"
I''ve defined the League table with ''has_many
:divisions''
I''ve defined the Division table with ''belongs_to
:league''
Here''s a snippet from my divison new.rhtml
<form action="/division/new" method="post">
<input id="league_id" name="division[league_id]"
value="<%@league.id %>"/>
<p>
<b>Division Name</b><br/>
<input id="divisionName"
name="division[divisionName]" size="30"
type="text" value=""/>
</p>
You can see I have included league_id as an input field and have passed
the value
as a parameter in the url.(which is working fine, it shows up in the
input field).
When I click on the save button, that''s when I get the error above.
I''m not sure
what it''s trying to tell me let alone what to do to fix it.
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Is your league_id defined as a foreign key? It seems like it''s complaining about not finding the league - are you trying to find the league in your new method of your controller? I would assume in the new method you would have something like @league=League.find(params[:league_id]) What file is the error referring to, the form or the controller? --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I do have league_id setup as a foreign key.
It''s complaining about my division controller. I do have what you
mention regarding the league parm. Here is a copy of the controller.
class DivisionController < ActionController::Base
layout "division-layout"
scaffold :division
before_filter :initLeague
def index
list
render_action ''list''
end
def new
@division = @league.divisions.new
end
def list
@divisions = @league.divisions.find_all
end
def show
@division = @league.divisions.find(params[:id])
end
private
def initLeague
@league = League.find(params[:league_id])
@league_name = @league.txtLeagueName
end
end
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> > private > def initLeague > @league = League.find(params[:league_id]) > @league_name = @league.txtLeagueName > end > endI think the problem lies in initLeague, where you are passing league_id as a parameter in the URL it is fine yes? However when you submit the form, to get the league ID you will have to get params[:division][:league_id]. I don''t think its a good idea calling initLeague in before_filter, you would be better calling it from within the controller methods where it is needed. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David Fitzgibbon wrote:>> >> private >> def initLeague >> @league = League.find(params[:league_id]) >> @league_name = @league.txtLeagueName >> end >> end > > I think the problem lies in initLeague, where you are passing league_id > as a parameter in the URL it is fine yes? However when you submit the > form, to get the league ID you will have to get > params[:division][:league_id]. > > I don''t think its a good idea calling initLeague in before_filter, you > would be better calling it from within the controller methods where it > is needed.I''d add to that by saying that not all of those methods will actually have a league_id passed as a param, so the before_filter will fail often. A. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---