Search code examples
ruby-on-railsarraysupdate-attributes

array of appointments being chosen instead of one appointment


Okay so I am trying to allow a student to choose an appointment with a professor from a table of available appointments. when I press "choose" it takes all the appointment_id's from the table and then puts the first number in the table that shows chosen appointments. so instead of choosing appointment id 5 it chooses 2, 4, 5, 6. Then it puts 2 in the chosen appointments table. What I want it to do is choose the appointment that I actually selected.

HERE IS MY appointments_available VIEW(I'm only including the necessary code):

<% @appointment.each do |appointment|%>
<tr>
  <td><%= appointment.professor_id %></td>
  <td><%= appointment.student_id %></td>
  <td><%= appointment.timeslot %></td>
  <td><%= link_to 'Choose', :controller => "appointments", :action => "student_edit", :id => @appointment %></td>
</tr>
<% end %>

HERE IS THE appointments CONTROLLER(again, not all the code is there):

def appointments_available
  @appointment = Appointment.find_all_by_student_id("")
end

def student_edit
  @appointment = Appointment.find_by_id(params[:id])
  @appointment.update_attribute(:student_id, session[:student].user_id)
end

Any Help is appreciated


Solution

  • So your actual issue is in your loop:

    <td><%= link_to 'Choose', :controller => "appointments", :action => "student_edit", :id => @appointment %></td>
    

    should be:

    <td><%= link_to 'Choose', :controller => "appointments", :action => "student_edit", :id => appointment.id %></td>
    

    Most importantly, you should try to be more RESTful and stick with the standard actions in your rails controllers, your senario does not represent the need to not follow best practices.

    <% @appointments.each do |appointment|%>
    <tr>
      <td><%= appointment.professor_id %></td>
      <td><%= appointment.student_id %></td>
      <td><%= appointment.timeslot %></td>
      <td><%= link_to 'Choose', edit_appointment_path(appointment) %></td>
    </tr>
    <% end %>
    

    If you are listing a collection I'd name your instance variable plurally. Any use of find_all_by_ will return an array and in your case, probably a collection from Arel. Also this appears to be your index action, so I'd try to use it as such.

    def index
      @appointments = Appointment.all
    end
    

    You used the method name appointments_available, if you have a scope of available I'd move that into your model so you then you could call Appointment.available.

    In your edit action you didn't need to use _by_id find already will find by.

    @appointment = Appointment.find(params[:id])
    

    But before you just start implementing my changes be sure to understand how REST works, and maybe generate a scaffold to see how a generic model would be setup, in all honesty when thinking about resources most of the time your situation will use the regular RESTful actions.

    It's not like against the rules to name your actions something different, but you lose a lot of the conventions granted to you by Rails' opinionated design.

    After you make your scaffold, take a peek at your routes by running rake routes, you'll see what options you have for routes. For example if you have an edit action in your controller, you'll also have an edit_appointment route which you can use by passing the appointment you want to edit to it like so: edit_appointment_path(@appointment).