When the user clicks submit
how can the info from two different models/DB tables be passed?
The user should be able to create a note
in the missed_dates
form and then that note should be saved to the respective @challenge
the missed date is referring to.
missed_dates/form.html.erb
<%= simple_form_for(@missed_date, url: challenge_missed_dates_path({ routine_id: @challenge }), remote: request.xhr?, html: { data: { modal: true } }) do |a| %>
<%= form_for [@notable, @note] do |b| %>
<%= a.text_field :one %>
<%= b.text_field :two %>
<%= button_tag(type: 'submit') do %>
Save
<% end %>
<% end %>
<% end %>
missed_date.rb
class MissedDate < ActiveRecord::Base
belongs_to :user
belongs_to :challenge
end
missed_date_controller
def new
@challenge = current_user.challenges.find(params[:challenge_id])
@missed_date = current_user.missed_dates.build
@notable = @challenge
@note = Note.new
end
def create
challenge = current_user.challenges.find(params[:challenge_id])
challenge.missed_days = challenge.missed_days + 1
challenge.save
@missed_date = challenge.missed_dates.build(missed_date_params)
@missed_date.user = self.current_user
@missed_date.save
respond_modal_with @missed_date, location: root_path
flash[:alert] = 'Strike added'
end