【问题标题】:Gettin 500 Internal Server Error in rails on ajax request在 ajax 请求上,Rails 中出现 Gettin 500 Internal Server Error
【发布时间】:2013-01-08 00:03:27
【问题描述】:

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

错误信息

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

ActionView::Template::Error(缺少部分约会/约会 使用 {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}。搜索:* “/Users/gjores/Sites/Rails/verkstad_test/app/views”): 1: $('#appointments').append(''); app/views/appointments/create.js.erb:1:in _app_views_appointments_create_js_erb___2325925946390315228_70273089113920' app/controllers/appointments_controller.rb:16:increate'

控制器

 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) %>');

路线

    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

【问题讨论】:

    标签: jquery ruby-on-rails ajax railscasts


    【解决方案1】:

    你的堆栈跟踪显示

    Missing partial appointments/appointment
    

    所以看起来 Rails 正在尝试渲染一个名为约会/appointments.html 或约会/appointments.js 的部分

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

    如果没有,则创建它。

    但是我怀疑您正在尝试做的是显示您的约会,因为我认为您希望下面的代码更新页面上某些元素的 html

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

    我觉得你需要把这条线变红

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

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

    【讨论】:

    • 是的,我有一个约会.js.erb 文件,其他代码正在获取创建的实例并将其附加到 div。就像 railscasts #136 建议的那样。
    • $('#appointments').append('')。我假设这一行将 HTML 注入 DIV。如果是这样,那么您想要运行 HTML 部分 'appointment/appointment.html.erb 并且应该只是一个 html 视图部分。修改了我的答案
    • 谢谢!这是对部分初学者错误的命名......
    【解决方案2】:

    这是因为您在视图 appointments/new.html.erb 中渲染部分内容,而不是在控制器 create 方法 中。

    由于partial是在视图appointments/new.html.erb中定义的,所以对应的javascript view应该是appointments/new.js.erb

    【讨论】:

    • 当我将“create.js.erb”的名称更改为“new.js.erb”时出现此错误。 Completed 500 Internal Server Error in 10ms ActionView::MissingTemplate (缺少模板约会/创建,应用程序/创建与 {:locale=>[:en], :formats=>[:js, :html], :handlers=>[: erb, :builder, :coffee]}. 搜索:* "/Users/gjores/Sites/Rails/verkstad_test/app/views" ): app/controllers/appointments_controller.rb:16:in `create'
    • 我还没有查看您的路线。现在这似乎是一个路由问题。
    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 2016-05-30
    • 2018-12-29
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 2022-07-25
    相关资源
    最近更新 更多