Reputation: 1276
I'm using a CalendarHelper from here http://railscasts.com/episodes/213-calendars-revised
It displays the current month, which gets picked up like this:
class XyController < ApplicationController
@date ||= Date.today
I would like a form in the view, so I could set this date to any custom date. Something like this:
= simple_form_for(@date) do |f|
= f.input ???
I think I'm missing the main point how to set up a form for an instance object, maybe. How could I implement a form to set a date for an instance variable which I set in the controller?
Edit.:
calendar_helper.rb
module CalendarHelper
def calendar(date = Date.today, &block)
Calendar.new(self, date, block).table
end
class Calendar < Struct.new(:view, :date, :callback)
HEADER = %w[Hétfő Kedd Szerda Csütörtök Péntek Szombat Vasárnap]
START_DAY = :monday
delegate :content_tag, to: :view
def table
content_tag :table, class: "calendar" do
header + week_rows
end
end
def header
...
end
def week_rows
...
end
def day_cell(day)
...
end
def day_classes(day)
...
end
def weeks
...
end
end
end
in the view:
= calendar do |date|
= date.day
So in the helper there is date = Date.today, i would like to change this to a @date instance, and would like to set that in a simple show view, eg.:
profiles_controller.rb
def show
@date ||= Date.today
end
And in the view like:
form_for(@date) do |f| f.date_select :date
Something like this, but this obviously isn't working nor correct
Upvotes: 3
Views: 5257
Reputation: 54902
You don't have to use the form for this feature, you can use Javascript:
# view
<%= select_date(@date, {}, onchange: 'reload_with_date($(this))') %>
<%= javascript_tag do %>
# this function will get the date selected
# and reload the page with the params[:date]
window.reload_with_date = function(select_tag) {
var date = Date.parse( $(select_tag).val() ); # get the selected date
var current_url = window.location.href; # get the current URL
if(current_url.indexOf('?') > -1) { # if already GET params in URL
window.location.href = current_url += '&date=' + date.toString();
} else {
window.location.href = current_url += '?date=' + date.toString();
}
}
<% end %>
# controller
def calendar
@date = params[:date] || Date.today
# will set @date to Date.today if params[:date].nil?
Documentation about select_date:
Don't hesitate to ask about this if you need. Hope this helps!
Upvotes: 1
Reputation: 2283
I suspect that something similar to this would work
#view
= simple_form_for(@date) do |f|
= f.input :date
Upvotes: 1
Reputation: 96594
I use this approach:
- if @user.content_date
%br/
= f.text_field :content_date, id: 'datepicker', size: 10
%br/
with this id being used to pick it.
Upvotes: 0
Reputation: 20938
Using form_for
with a variable is used to bind a form to an object, meaning each field of the form corresponds to a field of the object.
For instance, let's say you have a user field with a username and a age field, you could do the following :
Controller
@user = User.find(params[:id])
View
<%= form_for(@user) do |f| %>
<%= f.text_field :username %>
<%= f.text_field :age %>
<% end %>
The username and age fields will automatically be set to the values of the current user.
Now, for your case.
(I'm relying on the old calendars episode on railscast here : http://railscasts.com/episodes/213-calendars )
If your form is logically bound to an active record, you can use the calendar_for
helper as if you were using a form_for
helper.
So if your object is a user :
calendar_for @user, :year => @date.year, :month => @date.month
Otherwise, you'll have to use a normal form_tag
helper without an object bound to it, and use the calendar_date_select_tag
helper for inserting your calendar.
More infos on this helper in the docs
Feel free to ask any follow up questions.
Upvotes: 0
Reputation: 1073
<%= f.date_select(:dob,:start_year => 1950) %>
this should work for you. This is how I wrote in .html.erb
you can write it your way. But this is how a date picker can be generated.
Upvotes: 0