Gettin 500 Internal Server Error in rails on ajax request

13,940

Solution 1

Your stack trace shows

Missing partial appointments/appointment

So it looks like rails is trying to render a partial called appointments/appointments.html or appointments/appointments.js

Does a file called appointments.js.erb or appointments.html.erb exist?

If not then create it.

I however suspect that what you are trying to do is show your appointment as I think you want the code below to update the html of some element on your page

$('#appointments').append('<%= j render(@appointment) %>');

I think you need this line to red

$('#appointments').append('<%= j render :partial => 'appointments/appointment', :formats => :html %>');

Your html view partial should be appointments/_appointment.html.erb

Solution 2

This is because you are rendering a partial in a view appointments/new.html.erb and not in the controller create method .

Since the partial is defined in the view appointments/new.html.erb, so the corresponding javascript view should be appointments/new.js.erb.

Share:
13,940
Petter
Author by

Petter

Updated on August 22, 2022

Comments

  • Petter
    Petter over 1 year

    I'm trying to create a record from a form. I've used railscast 136 as ground work. When I trying to submit I get a 500 error for missing partial. I've created the controller correlated javascript view but it is requesting a view with the same name as the model.

    error message

    Rendered appointments/create.js.erb (3.8ms) Completed 500 Internal Server Error in 12ms

    ActionView::Template::Error (Missing partial appointments/appointment with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/Users/gjores/Sites/Rails/verkstad_test/app/views" ): 1: $('#appointments').append('<%= j render(@appointment) %>'); app/views/appointments/create.js.erb:1:in _app_views_appointments_create_js_erb___2325925946390315228_70273089113920' app/controllers/appointments_controller.rb:16:increate'

    Controller

     def create
        @appointment = Appointment.new(params[:appointment])
        if @appointment.save
          respond_to do |format|
            format.html { redirect_to new_appointment_path, :notice => "Successfully created appointment." }
            format.js
          end
        else
          render :action => 'new'
        end
      end
    

    appointments/new.html.erb

    <div id="appointments">
        <%= render 'shared/appointment_part' %>
    </div>
    <% title "New Appointment" %>
    <table>
    <% @students.each do |s| %>
    <%= form_for @appointment,  :remote => true do |f|%>
      <%= f.error_messages %>
      <tr>  
        <td><%= s.name %></td>
        <td><%= f.label :week %></td>
        <td><%= f.number_field :week %></td>
        <td><%= f.label :teacher_id %></td>
        <td><%= f.collection_select(:teacher_id, Teacher.all, :id, :name) %></td>
        <%= f.hidden_field :student_id, :value => s.id %>
    
      <td><%= f.submit %></td>
      </tr>
    <% end %>
    <% end -%>
    </table>
    
    <p><%= link_to "Back to List", appointments_path %></p>
    

    appointments/create.js.erb

    $('#appointments').append('<%= j render(@appointment) %>');
    

    Routes

        appointments GET    /appointments(.:format)          appointments#index
                     POST   /appointments(.:format)          appointments#create
     new_appointment GET    /appointments/new(.:format)      appointments#new
    edit_appointment GET    /appointments/:id/edit(.:format) appointments#edit
         appointment GET    /appointments/:id(.:format)      appointments#show
                     PUT    /appointments/:id(.:format)      appointments#update
                     DELETE /appointments/:id(.:format)      appointments#destroy
            teachers GET    /teachers(.:format)              teachers#index
                     POST   /teachers(.:format)              teachers#create
         new_teacher GET    /teachers/new(.:format)          teachers#new
        edit_teacher GET    /teachers/:id/edit(.:format)     teachers#edit
             teacher GET    /teachers/:id(.:format)          teachers#show
                     PUT    /teachers/:id(.:format)          teachers#update
                     DELETE /teachers/:id(.:format)          teachers#destroy
     notice_students POST   /students/notice(.:format)       students#notice
            students GET    /students(.:format)              students#index
                     POST   /students(.:format)              students#create
         new_student GET    /students/new(.:format)          students#new
        edit_student GET    /students/:id/edit(.:format)     students#edit
             student GET    /students/:id(.:format)          students#show
                     PUT    /students/:id(.:format)          students#update
                     DELETE /students/:id(.:format)          students#destroy
    
  • Petter
    Petter over 11 years
    When i changed the name of the "create.js.erb" to "new.js.erb" i get this error. Completed 500 Internal Server Error in 10ms ActionView::MissingTemplate (Missing template appointments/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/Users/gjores/Sites/Rails/verkstad_test/app/views" ): app/controllers/appointments_controller.rb:16:in `create'
  • My God
    My God over 11 years
    I haven't looked at your routes. This is now seems like a routing issue.
  • Petter
    Petter over 11 years
    Yes i got a appointments.js.erb file and the other code is taking the created instance and appending it to a div. just like railscasts #136 suggests.
  • Mark Stratmann
    Mark Stratmann over 11 years
    $('#appointments').append('<%= j render(@appointment) %>'). I assume this line is injecting HTML into a DIV. If so then you want to be running a HTML partial 'appointment/appointment.html.erb and that should just be an html view partial. Have amended my answer
  • Petter
    Petter about 11 years
    Thank you! It was a naming of partials beginners misstake…