0

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.

Eojo
  • 35
  • 8
  • if you check your route inside link_to holiday_schedule_connect_ranking_path(@holiday_schedule.id, nurse.id, first: @nurse.first, last: @nurse.last), method: :patch %> , you using method: :patch, this meaning it will route to update method, meanwhile inside your routes.rb you still provide it, the problem seem your method: ... – widjajayd Dec 19 '20 at 05:34

1 Answers1

0

Although the nurse build fails in create, it still remains an iterable part of the nurses collection. When the loop tries to turn the build into a link, an error occurs because the unsaved nurse object has no id. The solution was to check for persistence in the loop:

<ul>
  <% @nurses.each do |nurse| %>
    <% if nurse.persisted? %>
      <li>
      <%= link_to "#{nurse.first} #{nurse.last}", holiday_schedule_connect_ranking_path(params[:holiday_schedule_id], nurse.id, first: @nurse.first, last: @nurse.last), method: :patch %>
      </li>
    <% end %>
  <% end %>
</ul>
Eojo
  • 35
  • 8