动态地改变股利含量的Rails 4.1.4 [英] Dynamically Change Div Content in Rails 4.1.4
问题描述
我已经花了我差不多一整天试图找出如何解决这一难题,但不幸的是,大多数我发现都涉及到仍然允许渲染,在资产使用过时的Rails版本的解决方案。
这里就是我想要做的:
我的观点认为每个旅行项并显示各为页面上的缩略图。当用户点击每个缩略图,我想的更多的细节(以及关联,每次出行都有的has_many:周)事业部将呈现以下这些缩略图(更换previous内容)。
我似乎无法得到阿贾克斯工作,几个小时的尝试终于了解到,渲染不能在资产使用后。我衷心AP preciate任何帮助,以及潜在的解决方案,如果有人可以提供Ajax的一个可能的参考指南,使用Rails 4,将是非常美妙的,因为我似乎无法找到一个。
下面是我的code:
查看 - index.html.erb
< DIV>
< UL类=缩略图COL-MD-偏移2>
<%Trip.find_each办|行程| %>
<李类=COL-MD-3的风格=列表样式类型:无;>
<%=的link_to image_tag(http://placehold.it/350x350:边境= 0),:类=> 缩略图,:URL =>/车次/ selected_trip / PARAMS',:远程=>真%>
&其中; H3>&其中;%= trip.name%GT;&所述; / H3>
< /李>
<%结束%GT;
< / UL>
< DIV ID =selected_trip>
< / DIV>
< / DIV>
控制器 - trips.controller.rb
类TripsController<的ApplicationController
的before_filter:的authenticate_user!
after_action:verify_authorized
高清指数
@trips = Trip.all
授权之旅
结束
高清新
@trip = Trip.new
授权之旅
结束
DEF创建
@trip = Trip.new(trip_params)
授权之旅
如果@ trip.save
闪光[:通知] =新行程已创建。
redirect_to时@trip
其他
#Fill我
结束
结束
高清编辑
@trip = Trip.find(PARAMS [:ID])
授权之旅
结束
DEF更新
@trip = Trip.find(PARAMS [:ID])
授权之旅
@ trip.update(trip_params)
闪光[:通知] =之旅已被更新。
redirect_to时@trip
结束
高清节目
@trip = Trip.find(PARAMS [:ID])
授权之旅
结束
DEF破坏
@trip = Trip.find(PARAMS [:ID])
授权之旅
@ trip.destroy
闪光[:通知] =之旅已被删除。
redirect_to时trips_path
结束
高清selected_trip
@trip = Trip.find(PARAMS [:ID])
@ trip.name
respond_to代码做|格式|
format.js
结束
结束
结束
私人
高清trip_params
params.require(:跳闸).permit(:名称,说明,:状态)
结束
结束
使用Javascript - trips.js.erb(我知道这个方法已经不具有渲染不是提供资产工作)
$('#selected_trip)HTML(<%= escape_javascript(渲染:部分=>selected_trip',:CONTENT_TYPE =>text / html的'% >中)
部分 - _selected_trip.html.erb
< P>成功<!/ P> < - !只是用于测试,将实际内容替换 - >
谢谢, 内特
编辑下午11:10(它的工作原理) - 我已经改变了我的控制器:
高清selected_trip
@trip = Trip.find(PARAMS [:ID])
授权之旅
渲染:部分=> selected_trip',:CONTENT_TYPE => text / html的'
结束
和我的看法为:
< DIV>
< UL类=缩略图COL-MD-偏移2>
<%Trip.find_each办|行程| %>
<李类=COL-MD-3的风格=列表样式类型:无;数据TRIPID =&其中;%= trip.id%>中>
<%=的link_to image_tag(http://placehold.it/350x350:边境= 0),selected_trip_trip_path(旅),:类=> 缩略图,:远程=>真%>
&其中; H3>&其中;%= trip.name%GT;&所述; / H3>
< /李>
<%结束%GT;
< / UL>
< DIV ID =selected_trip>
< / DIV>
< / DIV>
< / DIV>
<脚本>
$('a.thumbnail')上(AJAX:成功',功能(EVT,数据){
VAR的目标= $('#selected_trip');
$(目标)。html的(数据);
});
< / SCRIPT>
如果你想避免资产的渲染,你可以尝试做这种方式。
我假设你知道你需要把听者为了赶AJAX调用,也可以计算出,在那里你要放置结果当AJAX回来与成功状态。然后,你想要做这样的事情:
$('whatever_container_they_can_click')上(AJAX:成功',功能(EVT,数据){
VAR的目标= $('#selected_trip'); //找到哪里是目标
$(目标)。html的(数据);
});
更改您的控制器行动:
高清selected_trip
@trip = Trip.find(PARAMS [:ID])
渲染:部分=> selected_trip',:CONTENT_TYPE => text / html的'
结束
I've spent almost my entire day trying to figure out how to solve this dilemma but unfortunately a majority of the solutions I've found are related to an outdated Rails version that still allowed "render" to be used in assets.
Here's what I'm trying to do:
My view finds each "Trip" entry and displays each as a thumbnail on the page. When the user clicks on each thumbnail, I would like the additional details (and also associations, each trip has a has_many: weeks) to be rendered in the Div below those thumbnails (replacing the previous content).
I can't seem to get Ajax to work and after several hours of attempting finally learned that "render" can't be used in assets. I would sincerely appreciate any help and along with potential solutions if someone could provide a possible reference guide for Ajax with Rails 4 that would be fantastic because I can't seem to find one.
Here's my code:
View - 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>
Controller - 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 (I know this method doesn't work anymore with render not being available in assets)
$('#selected_trip').html("<%= escape_javascript(render :partial => 'selected_trip', :content_type => 'text/html'%>")
Partial - _selected_trip.html.erb
<p>Success!</p> <!-- Just for testing, will replace with actual content -->
Thanks, Nate
Edit 11:10PM (it works)- I've changed my controller to:
def selected_trip
@trip = Trip.find(params[:id])
authorize Trip
render :partial => 'selected_trip', :content_type => 'text/html'
end
and my view to:
<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>
If you want to avoid rendering from the assets, you can try doing it this way.
I am assuming you know where you need to put the listener in order to catch the AJAX call, and also you can figure out, where you want to place the results when the AJAX comes back with a success status. Then, you want to do something like this:
$('whatever_container_they_can_click').on('ajax:success', function(evt, data) {
var target = $('#selected_trip'); // Find where is the destination
$(target).html(data);
});
Change your controller action to:
def selected_trip
@trip = Trip.find(params[:id])
render :partial => 'selected_trip', :content_type => 'text/html'
end
这篇关于动态地改变股利含量的Rails 4.1.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!