Jquery 使用Ajax在Rails上加载数据时缺少模板错误
我试图使用AJAX加载保存的数据,以避免刷新整个网页。我一直在看电视 当我将数据保存到渲染表单上时,问题就出现了。当我单击“保存”按钮时,什么也没发生,但在chrome developer工具上进行调试时,我发现错误Jquery 使用Ajax在Rails上加载数据时缺少模板错误,jquery,ruby-on-rails,ajax,templates,routes,Jquery,Ruby On Rails,Ajax,Templates,Routes,我试图使用AJAX加载保存的数据,以避免刷新整个网页。我一直在看电视 当我将数据保存到渲染表单上时,问题就出现了。当我单击“保存”按钮时,什么也没发生,但在chrome developer工具上进行调试时,我发现错误ActionView::MissingTemplate位于#create。位置是我的模型,创建要创建的动作 我花了很多时间试图找出哪里出了问题,并且一遍又一遍地观看铁轨的抛掷,但没有任何结果。如果有人能帮忙,请 这是控制器文件位置\u controller.rb class Loca
ActionView::MissingTemplate位于#create
。位置是我的模型,创建要创建的动作
我花了很多时间试图找出哪里出了问题,并且一遍又一遍地观看铁轨的抛掷,但没有任何结果。如果有人能帮忙,请
这是控制器文件位置\u 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
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
<%= 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 />
在轨道上工作3.2.13
这是我的消息来源
任何帮助或建议都将受到欢迎,祝大家度过愉快的一天
编辑:对不起,我忘了截图
在
创建
操作中,您没有保存对象。您所要做的就是调用new
。您可能希望使用@location保存它。在调用new
后保存,或者使用create
方法:
@location = Location.create(params[:location])
然后你的重定向到@location
转到你的show
操作,该操作还需要一个show.html.erb
,而你似乎没有这个MissingTemplate
错误
哦,看到你的截图了,你没有第二个截图中所示的位置定义。由于您正在传递@location
它正在查找\u location.html.erb
请添加该部分并使用location来使用您的位置属性,而不是@location
。如果仍然给您带来问题,请报告。您的错误到底是什么?是的,请附上一个屏幕截图或粘贴错误的完整回溯。您好,感谢回复,此项目是使用脚手架生成器创建的。所有实际文件和视图都已创建并用于正确工作。是的,我还有show.html.erb文件。可能create.js.erb错误?是否将行@location=location.new(params[:location])
更改为@location=location.create(params[:location])
?因为您没有实际保存它,所以渲染器无法找到它。请更改该行并尝试一下。是的,我做了,但仍然收到相同的错误消息“缺少模板…”我是否将该行提示错误<代码>$(“#所有站点”).empty().append(“”)代码>哦,看到你的截图了,你没有如第二个截图中所示的位置
部分定义。由于您正在传递@location
,它正在查找\u location.html.erb
。请添加该部分,并使用位置
来使用位置
的属性,而不是@location
。如果仍然给您带来问题,请报告。修复,感谢您的帮助,一旦我创建了_location.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 />
<%= 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 %>
@location = Location.create(params[:location])