henrybourne-ee4meeAH724@public.gmane.org
2006-Aug-24 18:15 UTC
[Rails] Form ''select'' question
Hi, I''d like to make a select element in a form which has the names of
months for the user to select, but sends numeric values for each month
when the form is submitted.
At the moment I have something like this:
<%= select_tag(:month, options_for_select(%w{ 1 2 3 4 5 6 7 8 9 },
@params[:month])) %>
But I''d like it to display the month names instead of the numbers. Any
ideas?
Thanks
Henry
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
henrybourne-ee4meeAH724@public.gmane.org
2006-Aug-24 19:38 UTC
[Rails] Re: Form ''select'' question
Ok, I''ve figured out how to do that, so ignore that last message, but I
now have another harder problem! The calendar I''m using displays the
current month when the page is loaded and the month can be changed
using the select element. However I can''t get the select element to
show the current month when the page loads. This is my controller code:
class CalendarController < ApplicationController
before_filter :year, :month
def index
if @params[:month] == nil && (@params[:year]) == nil
@params[:month] = @month
@params[:year] = @year
end
end
private
def year
@year = Time.now.year
end
def month
@month = Time.now.month
end
end
///////////////////////////////////////////////////////////
And my view template:
<% calendar(:year => @params[:year].to_i, :month =>
@params[:month].to_i, :first_day_of_week => 1) do |d|
link_to d.mday, :action => ''day_detail'', :id =>
d.mday
end
%>
<%= form_tag(:action => :index) %>
<%= select_tag(:month, options_for_select([["Jan",
"1"], ["Feb",
"2"], ["Mar", "3"], ["Apr",
"4"], ["May", "5"], ["Jun",
"6"], ["Jul",
"7"], ["Aug", "8"], ["Sep",
"9"], ["Oct", "10"], ["Nov",
"11"], ["Dec",
"12"]], @params[:month])) %>
<%= select_tag(:year, options_for_select(%w{ 2006 2007 },
@params[:year])) %>
<%= submit_tag("Ok") %>
<%= end_form_tag %>
<%= @params[:year] %>
<%= @params[:month] %>
///////////////////////////////////////////////////////////
@params[:month] is set to the current month if there has been nothing
sent to it by the form. The calendar displays the correct month, as
does <%= @params[:month] %> at the bottom. Why does my select list not
showw the correct month?
Thanks
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Have you thought about using one of the Rails built in functions? http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M000469 On 8/24/06, henrybourne-ee4meeAH724@public.gmane.org <henrybourne-ee4meeAH724@public.gmane.org> wrote:> > Ok, I''ve figured out how to do that, so ignore that last message, but I > now have another harder problem! The calendar I''m using displays the > current month when the page is loaded and the month can be changed > using the select element. However I can''t get the select element to > show the current month when the page loads. This is my controller code: > > class CalendarController < ApplicationController > > before_filter :year, :month > > def index > if @params[:month] == nil && (@params[:year]) == nil > @params[:month] = @month > @params[:year] = @year > end > end > > private > > def year > @year = Time.now.year > end > > def month > @month = Time.now.month > end > > end > > /////////////////////////////////////////////////////////// > And my view template: > > <%> calendar(:year => @params[:year].to_i, :month => > @params[:month].to_i, :first_day_of_week => 1) do |d| > link_to d.mday, :action => ''day_detail'', :id => d.mday > end > %> > > <%= form_tag(:action => :index) %> > > <%= select_tag(:month, options_for_select([["Jan", "1"], ["Feb", > "2"], ["Mar", "3"], ["Apr", "4"], ["May", "5"], ["Jun", "6"], ["Jul", > "7"], ["Aug", "8"], ["Sep", "9"], ["Oct", "10"], ["Nov", "11"], ["Dec", > "12"]], @params[:month])) %> > <%= select_tag(:year, options_for_select(%w{ 2006 2007 }, > @params[:year])) %> > <%= submit_tag("Ok") %> > > <%= end_form_tag %> > > <%= @params[:year] %> > <%= @params[:month] %> > > /////////////////////////////////////////////////////////// > > @params[:month] is set to the current month if there has been nothing > sent to it by the form. The calendar displays the correct month, as > does <%= @params[:month] %> at the bottom. Why does my select list not > showw the correct month? > > Thanks > > > > >-- Sterling Anderson sterling.anderson [at] gmail.com http://sterlinganderson.net/ 608.239.8387 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---