I have a form that creates a new nurse and a list of links below it which are created from the existing nurses. The list renders fine on first load, but after a failed save of the form, I get an error indicating I'm missing the nurse_id part of the links:
No route matches {:action=>"update", :controller=>"connect_rankings", :first=>"Wada", :holiday_schedule_id=>6, :id=>nil, :last=>"Lotta"}, possible unmatched constraints: [:id]
This is the code that creates the list of links:
<ul>
<% @nurses.each do |nurse| %>
<li>
<%= link_to "#{nurse.first} #{nurse.last}", holiday_schedule_connect_ranking_path(@holiday_schedule.id, nurse.id, first: @nurse.first, last: @nurse.last), method: :patch %>
</li>
<% end %>
</ul>
This code does work when re-rendered, so I figure it must have something to do with the loop:
<ul>
<% nurse = @nurses.first %>
<li>
<%= link_to "#{nurse.first} #{nurse.last}", holiday_schedule_connect_ranking_path(@holiday_schedule.id, nurse.id, first: @nurse.first, last: @nurse.last), method: :patch %>
</li>
</ul>
Just in case, here are the relevant controller actions.
def new
@holiday_schedule = HolidaySchedule.find(params[:holiday_schedule_id])
@nurses = @holiday_schedule.shift.nurses
@nurse = Nurse.new(first: params[:first], last: params[:last])
end
def create
@holiday_schedule = HolidaySchedule.find(params[:holiday_schedule_id])
@shift = @holiday_schedule.shift
@nurses = @shift.nurses
@nurse = @shift.nurses.build(nurse_params)
respond_to do |format|
if @nurse.save
@disconnected_rankings = @holiday_schedule.rankings.where('first = ? AND last = ?', @nurse.first, @nurse.last)
@nurse.connect_rankings(@disconnected_rankings);
format.html { redirect_to shift_path(@shift), notice: 'Set of rankings resolved.' }
format.json { redirect_to shift_path(@shift), notice: 'Set of rankings resolved.' }
else
format.html { render :new }
format.json { redirect_back fallback_location: root_path, notice: "Nurse couldn't be saved." }
end
end
end
Any help would be greatly appreciated.