hi..
I''m having a problem with an editing view to show a dropdown list
with selected value.
my tables are as followed
product table : id | Name | Price | Country_id
country table : id | CountryName
on the product new.htm.erb, I''ve got the list like this
<%= collection_select ("product", "Country_id",
@country, :id, :CountryName) %>
and it generates <select> html tags and saves good too.
now, I''d like to edit the product on the edit.html.erb with the same
dropdown list with
collected product.Country_id.
<option value = "{product.Country_id}" selected >
CountryName</option>
anyone knows how to to this? I''ve tried the rails.api way of doing
it.. such as
select(:product, :Country_id, country.find(:all).collect {|c|
[ c.CountryName, c.id ] }, {:include_blank => ''None''})
and it didn''t work..
Have you tried using the same collection_select statement on the edit page? It works for me. On May 27, 6:24 am, Mike75 <youp....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hi.. > I''m having a problem with an editing view to show a dropdown list > with selected value. > > my tables are as followed > product table : id | Name | Price | Country_id > country table : id | CountryName > > on the product new.htm.erb, I''ve got the list like this > <%= collection_select ("product", "Country_id", > @country, :id, :CountryName) %> > and it generates <select> html tags and saves good too. > > now, I''d like to edit the product on the edit.html.erb with the same > dropdown list with > collected product.Country_id. > > <option value = "{product.Country_id}" selected > CountryName</option> > > anyone knows how to to this? I''ve tried the rails.api way of doing > it.. such as > > select(:product, :Country_id, country.find(:all).collect {|c| > [ c.CountryName, c.id ] }, {:include_blank => ''None''}) > and it didn''t work..
well.. Litwin, i''ve tried it.. and it shows the drop down list.. but not with the saved data in the selected option, instead, it shows the default value, the one on top of the list. it supposed to work just like that? is there something i''ve done wrong, then? On May 28, 1:11 am, "E. Litwin" <elit...-ur4TIblo6goN+BqQ9rBEUg@public.gmane.org> wrote:> Have you tried using the same collection_select statement on the edit > page? It works for me. > > On May 27, 6:24 am, Mike75 <youp....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > hi.. > > I''m having a problem with an editing view to show a dropdown list > > withselectedvalue. > > > my tables are as followed > > product table : id | Name | Price | Country_id > > country table : id | CountryName > > > on the product new.htm.erb, I''ve got the list like this > > <%= collection_select ("product", "Country_id", > > @country, :id, :CountryName) %> > > and it generates <select> html tags and saves good too. > > > now, I''d like to edit the product on the edit.html.erb with the same > > dropdown list with > > collected product.Country_id. > > > <option value = "{product.Country_id}"selected> CountryName</option> > > > anyone knows how to to this? I''ve tried the rails.api way of doing > > it.. such as > > > select(:product, :Country_id, country.find(:all).collect {|c| > > [ c.CountryName, c.id ] }, {:include_blank => ''None''}) > > and it didn''t work..
im fairly new to Ruby On Rails, but I had to do this and here how I
did it.
in my subscriber controller:
def edit
@subscriber = Subscriber.find(params[:id])
@offer = Offer.find(@subscriber.offer)
@offers = Offer.find(:all)
end
In my edit view:
<p>
Offer:
<select name="subscriber[offer_id]">
<% @offers.each do |offer| %>
<% if @offer==offer %>
<option value="<%= offer.id %>" selected>
<%= offer.name %>
<% else %>
<option value="<%= offer.id %>">
<%= offer.name %>
<%end%>
</option>
<% end %>
</select>
</p>
It may be not the most efficient way since Im fairly new to RoR or to
web programming in general. I just did a if.else checking when in the
list of offers I get the subscriber.offer and added a "selected" a
little bit like your were trying to do
On May 28, 10:09 am, Mike75
<youp....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> well..
> Litwin, i''ve tried it..
> and it shows the drop down list..
> but not with the saved data in the selected option, instead, it shows
> the default value, the one on top of the list.
>
> it supposed to work just like that?
> is there something i''ve done wrong, then?
>
> On May 28, 1:11 am, "E. Litwin"
<elit...-ur4TIblo6goN+BqQ9rBEUg@public.gmane.org> wrote:
>
> > Have you tried using the same collection_select statement on the edit
> > page? It works for me.
>
> > On May 27, 6:24 am, Mike75
<youp....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > > hi..
> > > I''m having a problem with an editing view to show a
dropdown list
> > > withselectedvalue.
>
> > > my tables are as followed
> > > product table : id | Name | Price | Country_id
> > > country table : id | CountryName
>
> > > on the product new.htm.erb, I''ve got the list like this
> > > <%= collection_select ("product",
"Country_id",
> > > @country, :id, :CountryName) %>
> > > and it generates <select> html tags and saves good too.
>
> > > now, I''d like to edit the product on the edit.html.erb
with the same
> > > dropdown list with
> > > collected product.Country_id.
>
> > > <option value = "{product.Country_id}"selected>
CountryName</option>
>
> > > anyone knows how to to this? I''ve tried the rails.api
way of doing
> > > it.. such as
>
> > > select(:product, :Country_id, country.find(:all).collect {|c|
> > > [ c.CountryName, c.id ] }, {:include_blank =>
''None''})
> > > and it didn''t work..
Here is a snippet of the code from one of my projects.
This same code is in both my new and edit forms.
<% form_for(@field) do |f| %>
...
<p>
<%= f.label :lookup_category_id %><br />
<% @lookup_categories = LookupCategory.find(:all, :order =>
"lookup_category_name")
f.collection_select(:lookup_category_id,
@lookup_categories, :id, :lookup_category_name, :prompt=>"Select a
Lookup category (optional)")
%>
</p>
...
<% end %>
On May 28, 7:09 am, Mike75
<youp....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> well..
> Litwin, i''ve tried it..
> and it shows the drop down list..
> but not with the saved data in the selected option, instead, it shows
> the default value, the one on top of the list.
>
> it supposed to work just like that?
> is there something i''ve done wrong, then?
>
> On May 28, 1:11 am, "E. Litwin"
<elit...-ur4TIblo6goN+BqQ9rBEUg@public.gmane.org> wrote:
>
> > Have you tried using the same collection_select statement on the edit
> > page? It works for me.
>
> > On May 27, 6:24 am, Mike75
<youp....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > > hi..
> > > I''m having a problem with an editing view to show a
dropdown list
> > > withselectedvalue.
>
> > > my tables are as followed
> > > product table : id | Name | Price | Country_id
> > > country table : id | CountryName
>
> > > on the product new.htm.erb, I''ve got the list like this
> > > <%= collection_select ("product",
"Country_id",
> > > @country, :id, :CountryName) %>
> > > and it generates <select> html tags and saves good too.
>
> > > now, I''d like to edit the product on the edit.html.erb
with the same
> > > dropdown list with
> > > collected product.Country_id.
>
> > > <option value = "{product.Country_id}"selected>
CountryName</option>
>
> > > anyone knows how to to this? I''ve tried the rails.api
way of doing
> > > it.. such as
>
> > > select(:product, :Country_id, country.find(:all).collect {|c|
> > > [ c.CountryName, c.id ] }, {:include_blank =>
''None''})
> > > and it didn''t work..
thanx to all
I''ve got my problem solved..
i didn''t change anything on my code..
but changed the type of the field on the table
product table : id | Name | Price | Country_id
country table : id | CountryName
since the "id" fields are the integer type,
I changed the type of the Country_id field from the string to the
integer..
(I was just blindly following and not thinking when the instruction
for building controllers)
duh!...
I hope, none of you make mistakes like i did..
.. so I''m revealing my dumb mistake like this one out in the
open... :)
well.. for the conclusion..
<%= collection_select ("product", "Country_id",
@country, :id, :CountryName) %>
this is it..
i don''t need to use "selected=>" option or whatsoever
options, for my
case.. the "County_id" or :Country_id,
the :method takes both the method or the value itself and applies for
editing..
happy "ruby on rails"ing
On 5월29일, 오전12시33분, Fred <frederic.co...@gmail.com>
wrote:> im fairly new to Ruby On Rails, but I had to do this and here how I
> did it.
>
> in my subscriber controller:
> def edit
> @subscriber = Subscriber.find(params[:id])
> @offer = Offer.find(@subscriber.offer)
> @offers = Offer.find(:all)
> end
>
> In my edit view:
>
> <p>
> Offer:
> <select name="subscriber[offer_id]">
> <% @offers.each do |offer| %>
> <% if @offer==offer %>
> <option value="<%= offer.id %>" selected>
> <%= offer.name %>
> <% else %>
> <option value="<%= offer.id %>">
> <%= offer.name %>
> <%end%>
> </option>
> <% end %>
> </select>
> </p>
>
> It may be not the most efficient way since Im fairly new to RoR or to
> web programming in general. I just did a if.else checking when in the
> list of offers I get the subscriber.offer and added a "selected"
a
> little bit like your were trying to do
>
> On May 28, 10:09 am, Mike75
<youp....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > well..
> > Litwin, i''ve tried it..
> > and it shows the drop down list..
> > but not with the saved data in the selected option, instead, it shows
> > the default value, the one on top of the list.
>
> > it supposed to work just like that?
> > is there something i''ve done wrong, then?
>
> > On May 28, 1:11 am, "E. Litwin"
<elit...-ur4TIblo6goN+BqQ9rBEUg@public.gmane.org> wrote:
>
> > > Have you tried using the same collection_select statement on the
edit
> > > page? It works for me.
>
> > > On May 27, 6:24 am, Mike75
<youp....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > > > hi..
> > > > I''m having a problem with an editing view to show
adropdownlist
> > > > withselectedvalue.
>
> > > > my tables are as followed
> > > > product table : id | Name | Price | Country_id
> > > > country table : id | CountryName
>
> > > > on the product new.htm.erb, I''ve got the list like
this
> > > > <%= collection_select ("product",
"Country_id",
> > > > @country, :id, :CountryName) %>
> > > > and it generates <select> html tags and saves good
too.
>
> > > > now, I''d like to edit the product on the
edit.html.erb with the same
> > > >dropdownlist with
> > > > collected product.Country_id.
>
> > > > <option value =
"{product.Country_id}"selected> CountryName</option>
>
> > > > anyone knows how to to this? I''ve tried the
rails.api way of doing
> > > > it.. such as
>
> > > > select(:product, :Country_id, country.find(:all).collect
{|c|
> > > > [ c.CountryName, c.id ] }, {:include_blank =>
''None''})
> > > > and it didn''t work..