使用Ajax在Rails上加载数据时缺少模板错误

时间:2013-07-10 13:04:26

标签: jquery ruby-on-rails ajax templates routes

我正在尝试使用AJAX加载保存的数据,以避免刷新整个网页。我一直在关注rails cast Jquery Ajax video episode 136

将数据保存在呈现的表单上时会出现问题。当我单击“保存”按钮时没有任何反应,但在chrome开发人员工具上进行调试时,我发现了错误ActionView::MissingTemplate in Locations#create。地点是我的模型,并创建要创建的动作。

我花了很多时间试图找出什么是错的,并一次又一次地看着铁轨没有任何结果。请有人帮忙。

这是控制器文件locations_controller.rb

class LocationsController < ApplicationController

  def index
    @locations = Location.all
    @json = Location.all.to_gmaps4rails 
  end 

  def new
    @location = Location.new
  end

  def edit
    @location = Location.find(params[:id])
  end

  def create
    @location = Location.new(params[:location])
     respond_to do |format|
        format.html { redirect_to @location, notice: 'Location was successfully created.' }
        format.js   
    end
  end

def show
    @location = Location.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @location }
    end
  end
  def update
    @location = Location.find(params[:id])

    respond_to do |format|
      if @location.update_attributes(params[:location])
        format.html { redirect_to @location, notice: 'Location was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @location.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @location = Location.find(params[:id])
    @location.destroy

    respond_to do |format|
      format.html { redirect_to locations_url }
      format.json { head :no_content }
    end
  end
end

这是模型文件location.rb

class Location < ActiveRecord::Base
  attr_accessible :address, :gmaps, :latitude, :longitude, :name
  validates_presence_of :name, :address

  acts_as_gmappable :check_process => false


  def gmaps4rails_address
    address
  end

   def gmaps4rails_infowindow  
  "<h1>#{self.name}</h1>"
  end

  def gmaps4rails_sidebar
   "<h4>#{name}</h4>" 
  end
end

new.js.erb文件

$('#new_link').hide().after('<%= j render("form") %>');

create.js.erb文件

$('#new_location').remove();
$('#new_link').show();
$('#allsites').empty().append('<%= j render(@location) %>'); 

index.html.erb

<%= gmaps("markers" => {"data" => @json}) %>  


<table>
  <tr>
    <th>Name</th>
    <th>Address</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @locations.each do |location| %>
  <tr>
    <td><%= location.name %></td>
    <td><%= location.address %></td>
    <td><%= link_to 'Show', location %></td>
    <td><%= link_to 'Edit', edit_location_path(location) %></td>
    <td><%= link_to 'Destroy', location, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>
<div id="allsites">
//here I want to load all the data, ignore the table above
  </div>


<%= link_to 'New Location', new_location_path, id:"new_link", remote: true%>

<br />

此外,:remote => true已添加到表单

部分_form.html.erb文件

<%= form_for(@location, :remote => true) do |f| %>
  <% if @location.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@location.errors.count, "error") %> prohibited this location from being saved:</h2>

      <ul>
      <% @location.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :address %><br />
    <%= f.text_field :address %>
  </div>
  <div class="field">
    <%= f.label :latitude %><br />
    <%= f.text_field :latitude %>
  </div>
  <div class="field">
    <%= f.label :longitude %><br />
    <%= f.text_field :longitude %>
  </div>
  <div class="field">
    <%= f.label :gmaps %><br />
    <%= f.check_box :gmaps %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

使用Rails 3.2.13 这是我的来源http://railscasts.com/episodes/136-jquery-ajax-revised

任何帮助或消化都会得到充实,每个人都度过美好的一天

编辑:对不起,我忘记了截图 HTML error 500 Crhome debug tool

Missing Template error

1 个答案:

答案 0 :(得分:2)

create操作中,您没有保存对象。您所做的只是打电话给new。您可能希望在调用@location.save后使用new保存它,或者使用create方法:

@location = Location.create(params[:location])

然后,您的redirect_to @location转到show行动,该行动还需要show.html.erb您似乎没有的行为,这可能就是您获得MissingTemplate的原因错误。

哦,看到您的屏幕截图,您没有按照第二个屏幕截图所示定位部分。由于您正在通过@location,因此需要_location.html.erb请添加该部分并使用位置来使用您的位置属性而不是@location。如果它仍然给您带来问题,请报告。