[英]Rails 4/ UJS - ActionController::UnknownFormat - provide user with a way to directly open page with modal already opened
在我的Ruby on Rails应用程序中,我通过Rails的UJS(不引人注目的JavaScript)使用Ajax。
这是完美的工作方式:当用户在“交易”页面上,然后单击某个文本链接时,将进行Ajax调用,并加载包含远程内容的模式。
但是,我想给用户一种方法,使其直接进入模式已打开的页面,而无需他加载“交易”页面,然后单击文本链接 。 例如,用户可能会收到一封电子邮件并单击该电子邮件,然后他将立即打开带有已打开的模式的“交易页面”。
但是它不起作用,当我采用模态的路线时(请参见下面的代码),并在浏览器中输入http://myapp.com/deals/:id/deal_opportunities_modal ,然后出现以下错误:
在29ms内完成406不可接受
ActionController :: UnknownFormat-ActionController :: UnknownFormat:actionpack(4.2.0)lib / action_controller / metal / mime_responds.rb:218:在respond_to' app/controllers/deals_controller.rb:70:in
show_opportunities'中
这是我的文件:
controller / deals_controller.rb
# used so that old urls created for deals redirects to the new ones created
# indeed with friendly_id, the url is taken from the title :/deals/title
# but if we edit the deal title to deal/title2, the old url was still working
# we need to redirect the old url
# source - github.com/norman/friendly_id/issues/385
before_filter :find_deal,
:only => [ :showcase ]
before_filter :ensure_canonical_deal_path!,
:only => [ :showcase ]
def showcase
@deal = Deal.friendly.find(params[:id])
respond_to do |format|
format.html # showcase.html.erb
format.json { render json: @deal }
end
end
def show_opportunities
@deal = Deal.friendly.find(params[:id])
@opportunity = Opportunity.where('deal_id = ? AND deal_type = ?',
@deal.id, "high tech").first
respond_to do |format|
format.js
end
end
protected
def find_deal
@deal = Deal.friendly.find params[:id]
end
def ensure_canonical_deal_path!
if request.path != deal_page_path(@deal)
redirect_to deal_page_path(@deal, :format => params[:format]), :status => :moved_permanently
return false
end
end
app / views / deals / showcase.html.erb
<% if @deal.present? %>
this is the beginning
<%= render 'deals/deal_info_zone' %>
this is the end
<% end %>
views / deals / _deal_info_zone.html.erb
<div id="zoneA">
<div style="color:red;padding-top: 150px;">
<%= link_to "view infos of the latest Opportunity", deal_opportunities_modal_path, remote: true %>
</div>
</div>
视图/交易/deal_opportunities_modal.js.erb
这是通过Ajax触发的模态:views / deals / opportunity_modal。 请注意这里,我如何在此处尝试通过交易,但未成功,因此该行
$('body').append('<%= j render partial: "deals/deal_opportunities_modal" %>');
$('#myModal').modal('show');
/app/views/deals/_deal_opportunities_modal.html.erb
现在是模态视图/内容:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
this the the latest opportunity: <%= @opportunity.name %> <br/>
</div>
</div>
</div>
</div>
routes.rb
match '/deals/:id', # this is the Deal page
to: 'deals#showcase',
via: 'get',
as: :deal_page
match '/deals/:id/opportunity_modal',
to: 'deals#show_opportunities',
via: 'get',
as: :deal_opportunities_modal
如何实现这一点,即如何创建一种路径/ URL,当用户使用该路径/ URL时,他将到达“交易”页面,并且已打开模式“ show_opportunites” 。
我听说过js-routes,但它似乎对其他用途很有用(即在JavaScript文件中使用Rails路由)。
如果我使用户能够使用已经打开的模式访问页面,是否还会存在安全问题?
您无法打开仅可作为JS使用的控制器操作的浏览器。
我的查看方式,因为您已经在交易页面上有了一个打开模式的按钮,所以只要他们点击正确的网址就可以触发它
使用具有单独的控制器动作的路由来调用同一视图,并存储变量以告知视图使用模式
像这样:
get '/deals/:id/opportunities', to: "deals#opportunities", as: deal_opportunities
showcase
相同视图的操作,但将变量传递给该视图以检查参数, @show_modal = true
def opportunities
@show_modal = true
@deal = Deal.friendly.find(params[:id])
respond_to do |format|
format.html { render action: "/deals/show" }
end
end
showcase
视图中,如果存在show_modal,则使用javascript触发视图中已有的按钮以手动调用模式 <%= if @show_modal %>
<script type="text/javascript">
$(window).load(function(){
$("a.deals-button").trigger("click");
});
</script>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.