2013-01-24 130 views
3

我試圖從窗體創建一條記錄。我使用了railscast 136作爲基礎工作。當我嘗試提交時,我得到一個缺少部分的500錯誤。我創建了與控制器相關的javascript視圖,但它正在請求一個與模型名稱相同的視圖。Gettin 500內部服務器錯誤在AJAX請求上的rails

錯誤消息

完成500內部 服務器錯誤12ms的

::的ActionView模板::錯誤

渲染約會/ create.js.erb(3.8ms)(缺少部分的約會/約會 with {:locale => [:en],:formats => [:js,:html],:handlers => [:erb, :builder,:coffee]}。Search in:* 「/ 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 
+0

有什麼堆棧跟蹤? – nemesisdesign

+0

更新了我的文章... – Petter

回答

5

你的堆棧跟蹤顯示

Missing partial appointments/appointment 

所以看起來軌正試圖使部分所謂的約會/ appointments.html或約會/ appointments.js

難道一個名爲appointments.js文件。 erb或appointmentments.html.erb存在?

如果沒有,然後創建它。

但我懷疑你正在嘗試做的是展示你的約會,因爲我想你想下面的代碼更新一些元素的HTML的頁面

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

我認爲你需要這條線對紅

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

你的HTML視圖部分應約會/ _appointment.html.erb

+0

是的,我得到了一個約會.js.erb文件,其他代碼正在創建實例並將其附加到一個div。就像#136建議的railscasts。 – Petter

+0

$('#appointmentments')。append('<%= j render(@appointment)%>')。我認爲這一行是將HTML注入到DIV中。如果是這樣,那麼你想運行一個HTML部分'約會/ appointment.html.erb,這應該只是一個HTML視圖部分。修改我的回答 –

+0

謝謝!這是部分初學者misstake的命名... – Petter

1

這是因爲要渲染的局部視圖中的約會/ new.html.erb而不是在控制器創建方法

由於局部視圖中的定義約會/ new.html.erb,所以相應的javascript 視圖約會/ new.js.erb

+0

當我將「create.js.erb」的名稱更改爲「new.js.erb」時,出現此錯誤。在10ms內完成500次內部服務器錯誤 ActionView :: MissingTemplate(使用{:locale => [:zh],::formats => [:js,:html],:handlers =>缺少模板約會/創建,應用程序/ [:erb,:builder,:coffee]}。搜索: *「/ Users/gjores/Sites/Rails/verkstad_test/app/views」 ): app/controllers/appointmentments_controller.rb:16: ' – Petter

+0

我還沒有看過你的路線。這現在看起來像一個路由問題。 –

+0

更新了我的路線... – Petter

相關問題