I'm a Rails novice, I've come across a little trouble and I think my problem is similar to this question, but doesn't get solved there (and doesn't seem to be best practice to use a hidden_field):
Turning a calendar into a form using Rails?
I want to build a calendar in which someone tracks their miles on a bicycle by entering the miles on the specific date in which they took the trip.
I'm using https://github.com/watu/table_builder to build my calendar, it looks like this:
index.html.erb
<%= calendar_for(@miles, :year => @date.year, :month => @date.month) do |t| %>
<%= t.head('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') %>
<%= t.day(:day_method => :day_logged) do |day, miles| %>
<%= day.day %>
<%= render 'form', :day => day %>
<% miles.each do |mile| %>
<h3><%= h(mile.distance) %></h3>
<% end %>
<% end %>
<% end %>
_form.html.erb
<%= form_for @mile, url: {action: "create"}, html: {class:"mile_form"} do |f| %>
<%= f.hidden_field :day_logged, day %>
<p>
<%= f.label :distance, "Miles ridden" %>
<%= f.number_field :distance %>
<%= f.submit "Add" %>
</p>
trips_controller.rb
def index
@mile = Mile.new
@miles = Mile.find(:all)
@date = params[:month] ? Date.parse("#{params[:month]}-01") : Date.today
end
def create
@mile = Mile.new(params[:distance])
if @mile.save
redirect_to root_path
else
## ???
end
end
mile.rb
validates :distance, presence: true, :numericality => {:only_integer => true}
validates :day_logged, presence: true
schema.rb
create_table "miles", force: true do |t|
t.integer "distance"
t.date "day_logged"
t.datetime "created_at"
t.datetime "updated_at"
end
I've tried the hidden field option and have been receiving this error:
NoMethodError in Trips#index
undefined method `merge' for Sun, 26 Jan 2014:Date
I'm pretty stumped over this, and I don't think this is the best way to be doing this anyway. Any help would be greatly appreciated.