我正在尝试使用 AJAX 加载保存的数据,以避免刷新整个网页。我一直在看rails cast Jquery Ajax video episode 136。
当我将数据保存在呈现的表单上时会出现问题。当我单击保存按钮时没有任何反应,但是在 chrome 开发人员工具上进行调试时,我发现了错误ActionView::MissingTemplate in Locations#create
。Locations 是我的模型,并创建了创建动作。
我花了很多时间试图找出问题所在,并一次又一次地观察铁轨,但没有任何结果。请如果有人可以提供帮助。
这是控制器文件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
任何帮助或建议都将不胜感激,祝大家有美好的一天
编辑:对不起,我忘记了截图