Ruby Rooikie
2012-Nov-17 17:26 UTC
Need help fixing an error populating a select box in the insert page but works in the editpage
Two Models Country and Bank. The below form is used for insert and edit
page works fine . f.collection_select works fine for edit page but throws
error for insert page. I think error it is trying to populate the selected
value in the combo box but country id is not available. Can you help me
fixing the issue? What is prompt used for?
Error
undefined method `map'' for nil:NilClass
Extracted source (around line *#23*):
20: <%= f.label :country_id %><br />
21:
22:
23: <%= f.collection_select :country_id, @countries, :id, :name, :prompt
=> "Test" %>
24:
25: <div class="actions">
26: <%= f.submit %>
class Bank < ActiveRecord::Base
attr_accessible :name,:country_id
belongs_to :country
validates :name, :presence => true
validates_presence_of :country
end
class Country < ActiveRecord::Base
attr_accessible :name
end
Form
<%= form_for(@bank) do |f| %>
<% if @bank.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@bank.errors.count, "error") %>
prohibited this
bank from being saved:</h2>
<ul>
<% @bank.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :country_id %><br />
<%= f.collection_select :country_id, @countries, :id, :name, :prompt
=> "Test" %>
<div class="actions">
<%= f.submit %>
</div>
<% 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/qf9xpNmAkjsJ.
For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2012-Nov-18 09:37 UTC
Re: Need help fixing an error populating a select box in the insert page but works in the editpage
On 17 November 2012 17:26, Ruby Rooikie <rubyrookie1-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Two Models Country and Bank. The below form is used for insert and edit page > works fine . f.collection_select works fine for edit page but throws error > for insert page. I think error it is trying to populate the selected value > in the combo box but country id is not available. Can you help me fixing the > issue? What is prompt used for? > > > > > Error > > undefined method `map'' for nil:NilClass > > Extracted source (around line #23): > > 20: <%= f.label :country_id %><br /> > 21: > 22: > 23: <%= f.collection_select :country_id, @countries, :id, :name, > :prompt => "Test" %>My guess is that @countries is nil (the clue is in the error message). Are you setting that up in the controller action for insert? Colin> 24: > 25: <div class="actions"> > 26: <%= f.submit %> > > class Bank < ActiveRecord::Base > attr_accessible :name,:country_id > belongs_to :country > validates :name, :presence => true > > validates_presence_of :country > end > > > class Country < ActiveRecord::Base > attr_accessible :name > end > > Form > > > > <%= form_for(@bank) do |f| %> > <% if @bank.errors.any? %> > <div id="error_explanation"> > <h2><%= pluralize(@bank.errors.count, "error") %> prohibited this bank > from being saved:</h2> > > <ul> > <% @bank.errors.full_messages.each do |msg| %> > <li><%= msg %></li> > <% end %> > </ul> > </div> > <% end %> > > <div class="field"> > <%= f.label :name %><br /> > <%= f.text_field :name %> > </div> > > <div class="field"> > <%= f.label :country_id %><br /> > > > <%= f.collection_select :country_id, @countries, :id, :name, :prompt => > "Test" %> > > > <div class="actions"> > <%= f.submit %> > </div> > <% 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/qf9xpNmAkjsJ. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 https://groups.google.com/groups/opt_out.
rubyrookie
2012-Nov-18 10:01 UTC
Re: Need help fixing an error populating a select box in the insert page but works in the editpage
Yes I am setting it up. i got it to work. :) Thanks On Saturday, November 17, 2012 9:26:54 AM UTC-8, Ruby Rooikie wrote:> > Two Models Country and Bank. The below form is used for insert and edit > page works fine . f.collection_select works fine for edit page but throws > error for insert page. I think error it is trying to populate the selected > value in the combo box but country id is not available. Can you help me > fixing the issue? What is prompt used for? > > > > > Error > > undefined method `map'' for nil:NilClass > > Extracted source (around line *#23*): > > 20: <%= f.label :country_id %><br /> > 21: > 22: > 23: <%= f.collection_select :country_id, @countries, :id, :name, :prompt => "Test" %> > 24: > 25: <div class="actions"> > 26: <%= f.submit %> > > class Bank < ActiveRecord::Base > attr_accessible :name,:country_id > belongs_to :country > validates :name, :presence => true > > validates_presence_of :country > end > > > class Country < ActiveRecord::Base > attr_accessible :name > end > > Form > > > > <%= form_for(@bank) do |f| %> > <% if @bank.errors.any? %> > <div id="error_explanation"> > <h2><%= pluralize(@bank.errors.count, "error") %> prohibited this > bank from being saved:</h2> > > <ul> > <% @bank.errors.full_messages.each do |msg| %> > <li><%= msg %></li> > <% end %> > </ul> > </div> > <% end %> > > <div class="field"> > <%= f.label :name %><br /> > <%= f.text_field :name %> > </div> > > <div class="field"> > <%= f.label :country_id %><br /> > > > <%= f.collection_select :country_id, @countries, :id, :name, :prompt > => "Test" %> > > > <div class="actions"> > <%= f.submit %> > </div> > <% 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/-BCYEFqHLdUJ. For more options, visit https://groups.google.com/groups/opt_out.
rubyrookie
2012-Nov-18 10:19 UTC
Re: Need help fixing an error populating a select box in the insert page but works in the editpage
Thanks I have it working now. On Saturday, November 17, 2012 9:26:54 AM UTC-8, Ruby Rooikie wrote:> > Two Models Country and Bank. The below form is used for insert and edit > page works fine . f.collection_select works fine for edit page but throws > error for insert page. I think error it is trying to populate the selected > value in the combo box but country id is not available. Can you help me > fixing the issue? What is prompt used for? > > > > > Error > > undefined method `map'' for nil:NilClass > > Extracted source (around line *#23*): > > 20: <%= f.label :country_id %><br /> > 21: > 22: > 23: <%= f.collection_select :country_id, @countries, :id, :name, :prompt => "Test" %> > 24: > 25: <div class="actions"> > 26: <%= f.submit %> > > class Bank < ActiveRecord::Base > attr_accessible :name,:country_id > belongs_to :country > validates :name, :presence => true > > validates_presence_of :country > end > > > class Country < ActiveRecord::Base > attr_accessible :name > end > > Form > > > > <%= form_for(@bank) do |f| %> > <% if @bank.errors.any? %> > <div id="error_explanation"> > <h2><%= pluralize(@bank.errors.count, "error") %> prohibited this > bank from being saved:</h2> > > <ul> > <% @bank.errors.full_messages.each do |msg| %> > <li><%= msg %></li> > <% end %> > </ul> > </div> > <% end %> > > <div class="field"> > <%= f.label :name %><br /> > <%= f.text_field :name %> > </div> > > <div class="field"> > <%= f.label :country_id %><br /> > > > <%= f.collection_select :country_id, @countries, :id, :name, :prompt > => "Test" %> > > > <div class="actions"> > <%= f.submit %> > </div> > <% 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/8Ci6CeC39agJ. For more options, visit https://groups.google.com/groups/opt_out.