使用Rails将日历转换为表单? [英] Turning a calendar into a form using Rails?
问题描述
问题:如何将日历转换为日期选择是否与日历日而不是当前日期匹配的表单?
Question: How do I turn a calendar into a form where the dateselect is rendered to match the calendar day instead of the current date?
我要做什么:
我想创建一个日历来显示骑车者的锻炼。
What I'm trying to do: I'd like to create a calendar that displays the cyclist's workouts.
- 在每个日历日内创建锻炼,自动呈现与指定日期匹配的锻炼日期
每次锻炼都有很多间隔。每个日历日有一个锻炼。
Each workout has many intervals. Each calendar day has one workout.
插件:
从 https://github.com/p8/table_builder (gem和plugin都不使用rails 3.1部署)
Plugins: Extracted the table_builder.rb and calendar_helper.rb from https://github.com/p8/table_builder (both gem and plugin do not deploy with rails 3.1)
class WorkoutsController.rb
class WorkoutsController.rb
def new
@workout = Workout.new
@date = params[:month] ? Date.parse(params[:month].gsub('-', '/')) : Date.today
3.times do
@workout.intervals.build
end
end
Workout.rb
Workout.rb
attr_accessible :workoutdate, :intervals_attributes
has_many :intervals, :dependent => :destroy
accepts_nested_attributes_for :intervals, :reject_if => lambda { |a| a[:interval_name].blank? }, :allow_destroy => true
Interval.rb
Interval.rb
attr_accessible :interval_name, :workout_id
belongs_to :workout
/workouts/new.html.erb
views/workouts/new.html.erb
<div id ="calendar">
<%= calendar_for @workouts, :year => @date.year, :month => @date.month do |calendar| %>
<%= calendar.head('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday') %>
<% calendar.day(:day_method => :workoutdate) do |date, workouts| %>
<%= date.day %>
<%= render 'form' %>
<% end %>
<% end %>
</div>
views / workouts / _form.html.erb
views/workouts/_form.html.erb
<%= nested_form_for @workout do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :workoutdate %><br />
<%= f.date_select :workoutdate %>
</p>
<%= f.fields_for :intervals %>
<p><%= f.link_to_add "Add interval", :intervals %></p>
<p><%= f.submit "Submit" %></p>
<% end %>
views / workouts / _interval_fields.html.erb
views/workouts/_interval_fields.html.erb
<p>
<%= f.label :interval_name, "Interval" %>
<%= f.text_field :interval_name %>
<%= f.link_to_remove "remove" %>
</p>
目前正在heroku http://billeee.herokuapp.com/workouts
currently up on heroku http://billeee.herokuapp.com/workouts
推荐答案
day
方法的块中的日期,您应该能够将其传递到部分:
Since you have the date available in the block of the day
method, you should be able to pass it on to the partial like so:
<% calendar.day(:day_method => :workoutdate) do |date, workouts| %>
<%= date.day %>
<%= render 'form', :date => date %>
<% end %>
然后您可以在部分表单中使用它,并设置日期选择的日期您刚刚传递的局部变量:
Then you can make use of it within the form partial and set the date of the date select via the local variable you have just passed in:
<%= f.date_select :workoutdate, :default => date %>
注意:这种方法看起来有点有趣,因为你总是为特别的一天。为什么不发送日期通过隐藏输入字段?用户是否应该更改日期?如果是这样,这不会破坏UI逻辑吗?
As a note: this method seems a bit funny, since you're rendering this form always for a particular day anyways. Why not send the date e.g. via a hidden input field? Is the user supposed to change the date? And if so, doesn't that break the UI logic?
这篇关于使用Rails将日历转换为表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!