Is replacement_id a foreign key to the ''replacements'' table? I generally find I''d use a list if I was going to have the user select from a list of replacements in the replacement table.. But I guess you want the user to just type in the id and have it store the association like that? hmm.. If you refer to a field of the billingprovider or an association (ie: replacement.. not replacement_id) directly it should be fine.. but I''ve never tried directly to the id using the form helpers. You may need to handle that stuff more manually to nicely catch the case where they enter a number that isn''t a valid key, etc.. (ie: using the text_field_tag helper and looking for the parameter in the update method) On Thursday 26 May 2005 05:26, Chris Armstrong wrote:> Hi > > I''m a newbie with ruby on rails and developing my first app. My problem is > that a text_field method should be prefilled with a value and is not. > > Actually i sought it''s following with form rendering: > > start_form_tag :action => ''update'', :id => @billingprovider > text_field "billingprovider", "replacement_id" > > :id says that the attributes of the billingprovider object should be > > checked for values. > > As example, if there is a value for billingprovider.replacment_id and the > form is rendered with text_field "billingprovider", "replacement_id" it > actually should automatically set this value as <input type="text" > value="the value from billingprovider.replacement_id">. > > The problem is that this is not happening and i don''t know why. Can > somebody help me and tell me what i''m doing wrong? > > > webmaster_controller.rb > ---------------------------------------- > def billing_codes > @webmaster = Webmaster.find(1) > @billingproviders = @webmaster.billingproviders > end > > billing_codes.rhtml > ---------------------------------------- > <% @billingproviders.each do |billingprovider| %> > <%= start_form_tag :action => ''update'', :id => @billingprovider %> > <table width="100%" border="0" cellspacing="0" cellpadding="3"> > <tr><td width="25%"><%= billingprovider.name %></td> > <td width="25%"><%= text_field "billingprovider", "replacement_id", "size" > => 15 %></td> > <td width="15%"><%= link_to "info", { :action => "billingprovider_info" }, > > :target => "_new" %></td> > > <td width="15%"><%= link_to "Get an ID", billingprovider.subscribe_url, > > :target => "_new" %></td> > > <td width="20%"><%= submit_tag "Save" %></td> > </tr> > </table> > <%= end_form_tag %> > <% end %> > > > > sql: > ---------------------------------------- > table billingproviders_webmasters > +--------------------+------------------+------+-----+---------+-------+ > > | Field | Type | Null | Key | Default | Extra | > > +--------------------+------------------+------+-----+---------+-------+ > > | webmaster_id | int(10) unsigned | | | 0 | | > | billingprovider_id | int(10) unsigned | | | 0 | | > | replacement_id | varchar(255) | YES | | NULL | | > > +--------------------+------------------+------+-----+---------+-------+ > > table webmasters > +------------+------------------+------+-----+---------+----------------+ > > | Field | Type | Null | Key | Default | Extra | > > +------------+------------------+------+-----+---------+----------------+ > > | id | int(10) unsigned | | PRI | NULL | auto_increment | > | referer_id | int(10) unsigned | YES | | NULL | | > | username | varchar(40) | YES | MUL | NULL | | > | password | varchar(40) | YES | | NULL | | > | email | varchar(255) | YES | MUL | NULL | | > | mailings | enum(''Y'',''N'') | YES | | Y | | > | active | enum(''Y'',''N'') | YES | | Y | | > | percentage | decimal(3,2) | | | 0.00 | | > | created_on | datetime | YES | | NULL | | > | updated_on | datetime | YES | | NULL | | > > +------------+------------------+------+-----+---------+----------------+ > > table billingproviders > +----------------+------------------+------+-----+---------+--------------- >-+ > > | Field | Type | Null | Key | Default | Extra > | | > > +----------------+------------------+------+-----+---------+--------------- >-+ > > | id | int(10) unsigned | | PRI | NULL | auto_increment > | | name | varchar(255) | YES | MUL | NULL | > | | description | text | YES | | NULL | > | | main_url | varchar(255) | YES | | NULL | > | | subscribe_url | varchar(255) | YES | | NULL | > | | replacement_id | varchar(255) | YES | | NULL | > | | created_on | datetime | YES | | NULL | > | | updated_on | datetime | YES | | NULL | > | | > > +----------------+------------------+------+-----+---------+--------------- >-+ > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hi I''m a newbie with ruby on rails and developing my first app. My problem is that a text_field method should be prefilled with a value and is not. Actually i sought it''s following with form rendering: start_form_tag :action => ''update'', :id => @billingprovider text_field "billingprovider", "replacement_id" :id says that the attributes of the billingprovider object should be checked for values. As example, if there is a value for billingprovider.replacment_id and the form is rendered with text_field "billingprovider", "replacement_id" it actually should automatically set this value as <input type="text" value="the value from billingprovider.replacement_id">. The problem is that this is not happening and i don''t know why. Can somebody help me and tell me what i''m doing wrong? webmaster_controller.rb ---------------------------------------- def billing_codes @webmaster = Webmaster.find(1) @billingproviders = @webmaster.billingproviders end billing_codes.rhtml ---------------------------------------- <% @billingproviders.each do |billingprovider| %> <%= start_form_tag :action => ''update'', :id => @billingprovider %> <table width="100%" border="0" cellspacing="0" cellpadding="3"> <tr><td width="25%"><%= billingprovider.name %></td> <td width="25%"><%= text_field "billingprovider", "replacement_id", "size" => 15 %></td> <td width="15%"><%= link_to "info", { :action => "billingprovider_info" }, :target => "_new" %></td> <td width="15%"><%= link_to "Get an ID", billingprovider.subscribe_url, :target => "_new" %></td> <td width="20%"><%= submit_tag "Save" %></td> </tr> </table> <%= end_form_tag %> <% end %> sql: ---------------------------------------- table billingproviders_webmasters +--------------------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------------+------------------+------+-----+---------+-------+ | webmaster_id | int(10) unsigned | | | 0 | | | billingprovider_id | int(10) unsigned | | | 0 | | | replacement_id | varchar(255) | YES | | NULL | | +--------------------+------------------+------+-----+---------+-------+ table webmasters +------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | | PRI | NULL | auto_increment | | referer_id | int(10) unsigned | YES | | NULL | | | username | varchar(40) | YES | MUL | NULL | | | password | varchar(40) | YES | | NULL | | | email | varchar(255) | YES | MUL | NULL | | | mailings | enum(''Y'',''N'') | YES | | Y | | | active | enum(''Y'',''N'') | YES | | Y | | | percentage | decimal(3,2) | | | 0.00 | | | created_on | datetime | YES | | NULL | | | updated_on | datetime | YES | | NULL | | +------------+------------------+------+-----+---------+----------------+ table billingproviders +----------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | | PRI | NULL | auto_increment | | name | varchar(255) | YES | MUL | NULL | | | description | text | YES | | NULL | | | main_url | varchar(255) | YES | | NULL | | | subscribe_url | varchar(255) | YES | | NULL | | | replacement_id | varchar(255) | YES | | NULL | | | created_on | datetime | YES | | NULL | | | updated_on | datetime | YES | | NULL | | +----------------+------------------+------+-----+---------+----------------+
> billing_codes.rhtml > ---------------------------------------- > <% @billingproviders.each do |billingprovider| %>This is the line that''s causing your woes. The object that your form helpers uses *must* be an instance variable--otherwise, the scope of your billingprovider variable will go out of scope as soon as the methods are invoked. Use this instead: <% @billingproviders.each do |@billingprovider| %>> <%= start_form_tag :action => ''update'', :id => @billingprovider %> > <table width="100%" border="0" cellspacing="0" cellpadding="3"> > <tr><td width="25%"><%= billingprovider.name %></td> > <td width="25%"><%= text_field "billingprovider", "replacement_id", > "size" > => 15 %></td>Throughout the rest of your code, make sure to use @billingprovider, except when you quote the name of that variable. I''ll fix the above 4 lines as an example: <%= start_form_tag :action => ''update'', :id => @billingprovider %> <table width="100%" border="0" cellspacing="0" cellpadding="3"> <tr><td width="25%"><%= @billingprovider.name %></td> <td width="25%"><%= text_field "billingprovider", "replacement_id", "size"=> 15 %></td> All the best, Duane Johnson (canadaduane)
replacment_id is an attribute of the table billingproviders_webmasters: create table billingproviders_webmasters ( billingprovider_id int(11), webmaster_id int(11), replacement_id varchar(80) ); create table webmaster ( id int(11) auto_increment not null, last_name varchar(80), firstname varchar(80), created_on datetime, updated_on datetime ); create table billingproviders ( id int(11) auto_increment not null, name varchar(80), created_on datetime, updated_on datetime ); I have found out what the problem was, but have another one now because i don''t know how to update the replacement_id in the billingproviders_webmasters table. The previous problem was, that billingprovider wasn''t an object: before: <% @billingproviders.each do |billingprovider| %> after: <% @billingproviders.each do |@billingprovider| %> Does somebody have a solution how to update the replacement_id value? You can see my tries within the update_billingcodes definition. What i''m trying to do in rails is following: update billingproviders_webmasters set replacement_id=''new value'' where webmaster_id=''1'' and billingprovider_id=''1''; My webmaster_controller looks like this: class WebmasterController < ApplicationController def index end def support end def billing_codes @webmaster = Webmaster.find(1) @billingproviders = @webmaster.billingproviders end def update_billingcode @webmaster = Webmaster.find(1) @billingprovider = @webmaster.billingproviders.find(@params[:id]) # if @billingprovider.update_attributes(@params[:billingprovider]) # flash[''notice''] = ''Billing Code was successfully updated.'' # redirect_to :action => ''billing_codes'' # else # render_action ''billing_codes'' # end end end Thanks Chris>> table billingproviders_webmasters >> +--------------------+------------------+------+-----+---------+-------+ >> >> | Field | Type | Null | Key | Default | Extra | >> >> +--------------------+------------------+------+-----+---------+-------+ >> >> | webmaster_id | int(10) unsigned | | | 0 | | >> | billingprovider_id | int(10) unsigned | | | 0 | | >> | replacement_id | varchar(255) | YES | | NULL | | >> >> +--------------------+------------------+------+-----+---------+-------+> Is replacement_id a foreign key to the ''replacements'' table? > > I generally find I''d use a list if I was going to have the user select > from a > list of replacements in the replacement table.. But I guess you want the > user > to just type in the id and have it store the association like that? hmm.. > > If you refer to a field of the billingprovider or an association (ie: > replacement.. not replacement_id) directly it should be fine.. but I''ve > never > tried directly to the id using the form helpers. You may need to handle > that > stuff more manually to nicely catch the case where they enter a number > that > isn''t a valid key, etc.. > > (ie: using the text_field_tag helper and looking for the parameter in the > update method) > > On Thursday 26 May 2005 05:26, Chris Armstrong wrote: >> Hi >> >> I''m a newbie with ruby on rails and developing my first app. My problem >> is >> that a text_field method should be prefilled with a value and is not. >> >> Actually i sought it''s following with form rendering: >> >> start_form_tag :action => ''update'', :id => @billingprovider >> text_field "billingprovider", "replacement_id" >> >> :id says that the attributes of the billingprovider object should be >> >> checked for values. >> >> As example, if there is a value for billingprovider.replacment_id and >> the >> form is rendered with text_field "billingprovider", "replacement_id" it >> actually should automatically set this value as <input type="text" >> value="the value from billingprovider.replacement_id">. >> >> The problem is that this is not happening and i don''t know why. Can >> somebody help me and tell me what i''m doing wrong? >> >> >> webmaster_controller.rb >> ---------------------------------------- >> def billing_codes >> @webmaster = Webmaster.find(1) >> @billingproviders = @webmaster.billingproviders >> end >> >> billing_codes.rhtml >> ---------------------------------------- >> <% @billingproviders.each do |billingprovider| %> >> <%= start_form_tag :action => ''update'', :id => @billingprovider %> >> <table width="100%" border="0" cellspacing="0" cellpadding="3"> >> <tr><td width="25%"><%= billingprovider.name %></td> >> <td width="25%"><%= text_field "billingprovider", "replacement_id", >> "size" >> => 15 %></td> >> <td width="15%"><%= link_to "info", { :action => "billingprovider_info" >> }, >> >> :target => "_new" %></td> >> >> <td width="15%"><%= link_to "Get an ID", billingprovider.subscribe_url, >> >> :target => "_new" %></td> >> >> <td width="20%"><%= submit_tag "Save" %></td> >> </tr> >> </table> >> <%= end_form_tag %> >> <% end %> >> >> >> >> sql: >> ---------------------------------------- >> table billingproviders_webmasters >> +--------------------+------------------+------+-----+---------+-------+ >> >> | Field | Type | Null | Key | Default | Extra | >> >> +--------------------+------------------+------+-----+---------+-------+ >> >> | webmaster_id | int(10) unsigned | | | 0 | | >> | billingprovider_id | int(10) unsigned | | | 0 | | >> | replacement_id | varchar(255) | YES | | NULL | | >> >> +--------------------+------------------+------+-----+---------+-------+ >> >> table webmasters >> +------------+------------------+------+-----+---------+----------------+ >> >> | Field | Type | Null | Key | Default | Extra >> | >> >> +------------+------------------+------+-----+---------+----------------+ >> >> | id | int(10) unsigned | | PRI | NULL | auto_increment >> | >> | referer_id | int(10) unsigned | YES | | NULL | >> | >> | username | varchar(40) | YES | MUL | NULL | >> | >> | password | varchar(40) | YES | | NULL | >> | >> | email | varchar(255) | YES | MUL | NULL | >> | >> | mailings | enum(''Y'',''N'') | YES | | Y | >> | >> | active | enum(''Y'',''N'') | YES | | Y | >> | >> | percentage | decimal(3,2) | | | 0.00 | >> | >> | created_on | datetime | YES | | NULL | >> | >> | updated_on | datetime | YES | | NULL | >> | >> >> +------------+------------------+------+-----+---------+----------------+ >> >> table billingproviders >> +----------------+------------------+------+-----+---------+--------------- >>-+ >> >> | Field | Type | Null | Key | Default | Extra >> | | >> >> +----------------+------------------+------+-----+---------+--------------- >>-+ >> >> | id | int(10) unsigned | | PRI | NULL | >> auto_increment >> | | name | varchar(255) | YES | MUL | NULL | >> | | description | text | YES | | NULL | >> | | main_url | varchar(255) | YES | | NULL | >> | | subscribe_url | varchar(255) | YES | | NULL | >> | | replacement_id | varchar(255) | YES | | NULL | >> | | created_on | datetime | YES | | NULL | >> | | updated_on | datetime | YES | | NULL >> | >> | | >> >> +----------------+------------------+------+-----+---------+--------------- >>-+ >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >
thx. duane. yes that was it.>> billing_codes.rhtml >> ---------------------------------------- >> <% @billingproviders.each do |billingprovider| %> > This is the line that''s causing your woes. The object that your form > helpers uses *must* be an instance variable--otherwise, the scope of > your billingprovider variable will go out of scope as soon as the > methods are invoked. Use this instead: > > <% @billingproviders.each do |@billingprovider| %> > > >> <%= start_form_tag :action => ''update'', :id => @billingprovider %> >> <table width="100%" border="0" cellspacing="0" cellpadding="3"> >> <tr><td width="25%"><%= billingprovider.name %></td> >> <td width="25%"><%= text_field "billingprovider", "replacement_id", >> "size" >> => 15 %></td> > > Throughout the rest of your code, make sure to use @billingprovider, > except when you quote the name of that variable. I''ll fix the above > 4 lines as an example: > > <%= start_form_tag :action => ''update'', :id => @billingprovider %> > <table width="100%" border="0" cellspacing="0" cellpadding="3"> > <tr><td width="25%"><%= @billingprovider.name %></td> > <td width="25%"><%= text_field "billingprovider", "replacement_id", > "size"=> 15 %></td> > > All the best, > Duane Johnson > (canadaduane) >