在ajax请求中的rails中的Gettin 500内部服务器错误

时间:2013-01-24 09:25:15

标签: jquery ruby-on-rails ajax railscasts

我正在尝试从表单创建记录。我使用railscast 136作为地面工作。当我尝试提交时,我因缺少部分而得到500错误。我已经创建了控制器相关的javascript视图,但它正在请求一个与模型同名的视图。

错误消息

  

呈现约会/ create.js.erb(3.8ms)已完成500内部   服务器错误12毫秒

     

ActionView :: Template ::错误(缺少部分约会/约会   with {:locale => [:en],:formats => [:js,:html],:handlers => [:erb,   :建造者,:咖啡]}。搜索范围:*   “/ Users / gjores / Sites / Rails / verkstad_test / app / views”):       1:$('#appointmentments')。append('<%= j render(@appointment)%>');应用程序/视图/约会/ create.js.erb:1:   _app_views_appointments_create_js_erb___2325925946390315228_70273089113920' app/controllers/appointments_controller.rb:16:in创建'

控制器

 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

约会/ 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>

约会/ create.js.erb

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

路线

    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

2 个答案:

答案 0 :(得分:5)

您的堆栈跟踪显示

Missing partial appointments/appointment

所以看起来rails试图呈现部分名为appointmentments / appointmentments.html或约会/约会.js

是否存在名为appointmentments.js.erb或appointmentments.html.erb的文件?

如果没有,则创建它。

我怀疑你要做的是显示你的约会,因为我认为你想要下面的代码来更新页面上某些元素的html

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

我认为你需要这一行为红色

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

你的html视图部分应该是约会/ _appointment.html.erb

答案 1 :(得分:1)

这是因为您在视图 appointmentments / new.html.erb 中渲染部分而不在控制器创建方法中渲染。

由于部分是在视图 appointmentments / new.html.erb 中定义的,因此相应的 javascript视图应为约会/ new.js.erb