【发布时间】:2014-08-18 03:18:22
【问题描述】:
我几乎一整天都在试图弄清楚如何解决这个难题,但不幸的是,我发现的大多数解决方案都与仍然允许在资产中使用“渲染”的过时 Rails 版本有关。
这是我想要做的:
我的视图找到每个“Trip”条目并将每个条目显示为页面上的缩略图。当用户单击每个缩略图时,我希望在这些缩略图下方的 Div 中呈现其他详细信息(以及关联,每次旅行都有一个 has_many: 周)(替换以前的内容)。
我似乎无法让 Ajax 工作,经过几个小时的尝试终于了解到“渲染”不能用于资产。如果有人可以提供有关 Rails 4 的 Ajax 的可能参考指南,我将真诚地感谢任何帮助和潜在的解决方案,这将是非常棒的,因为我似乎找不到。
这是我的代码:
查看 - index.html.erb
<div>
<ul class="thumbnails col-md-offset-2">
<% Trip.find_each do |trip| %>
<li class="col-md-3" style="list-style-type:none;">
<%= link_to image_tag("http://placehold.it/350x350", :border => 0), :class => 'thumbnail', :url=>'/trips/selected_trip/params', :remote => true %>
<h3><%= trip.name %></h3>
</li>
<% end %>
</ul>
<div id="selected_trip">
</div>
</div>
控制器 - trips.controller.rb
class TripsController < ApplicationController
before_filter :authenticate_user!
after_action :verify_authorized
def index
@trips = Trip.all
authorize Trip
end
def new
@trip = Trip.new
authorize Trip
end
def create
@trip = Trip.new(trip_params)
authorize Trip
if @trip.save
flash[:notice] = "New trip has been created."
redirect_to @trip
else
#Fill me in
end
end
def edit
@trip = Trip.find(params[:id])
authorize Trip
end
def update
@trip = Trip.find(params[:id])
authorize Trip
@trip.update(trip_params)
flash[:notice] = "Trip has been updated."
redirect_to @trip
end
def show
@trip = Trip.find(params[:id])
authorize Trip
end
def destroy
@trip = Trip.find(params[:id])
authorize Trip
@trip.destroy
flash[:notice] = "Trip has been deleted."
redirect_to trips_path
end
def selected_trip
@trip = Trip.find(params[:id])
@trip.name
respond_to do |format|
format.js
end
end
end
private
def trip_params
params.require(:trip).permit(:name, :description, :status)
end
end
Javascript - trips.js.erb(我知道这个方法不再适用于渲染在资产中不可用)
$('#selected_trip').html("<%= escape_javascript(render :partial => 'selected_trip', :content_type => 'text/html'%>")
部分 - _selected_trip.html.erb
<p>Success!</p> <!-- Just for testing, will replace with actual content -->
谢谢, 内特
晚上 11:10 编辑(有效)- 我已将控制器更改为:
def selected_trip
@trip = Trip.find(params[:id])
authorize Trip
render :partial => 'selected_trip', :content_type => 'text/html'
end
我的看法:
<div>
<ul class="thumbnails col-md-offset-2">
<% Trip.find_each do |trip| %>
<li class="col-md-3" style="list-style-type:none;" data-tripID="<%= trip.id %>">
<%= link_to image_tag("http://placehold.it/350x350", :border => 0), selected_trip_trip_path(trip), :class => 'thumbnail', :remote => true %>
<h3><%= trip.name %></h3>
</li>
<% end %>
</ul>
<div id="selected_trip">
</div>
</div>
</div>
<script>
$('a.thumbnail').on('ajax:success', function(evt, data) {
var target = $('#selected_trip');
$(target).html(data);
});
</script>
【问题讨论】:
-
您在哪里看到在 Rails 4 中无法从 JS 渲染部分内容?我认为这是一个错误的说法。
-
这个答案来自 2011 年。看看这个:stackoverflow.com/questions/18843185/rails-4-ajax-update-div
-
我确实看到了,但我认为我发现另一条评论提到它在 Rails 4 中是相同的。不管我再试一次,但得到了与以前类似的错误,“未定义的方法 `render' for #:0x007fbd079814a0>" used $('#selected_trip').html("") 通过控制器传递@trip = current_trip
-
检查我的答案。让我们看看这是否有帮助。如果您有多个容器来显示结果,您不希望它们具有相同的 id #selected_trip。您想以某种方式创建属性,让您能够唯一地识别创建 AJAX 请求的缩略图以及需要呈现响应的位置(查看数据属性)。
标签: javascript jquery ruby ajax ruby-on-rails-4