所以我有相当复杂的表格/要求:我的表格(由4个多选框组成)我希望在不同的页面上发布该表格=不同的控制器,根据点击的按钮 - 我已经3个按钮= 3页。 (在那些多选中有类别,我想从数据库中提取一些数据来累积到这些类别)。所以我现在看起来像那样:
var main = function(){
function proceed(mapType){
switch(mapType) {
case "showevent" : action = "events/index"; @page = events_path ; break; //index events
case "addplace" : action = "places/new"; @page = new_place_path; break;
case "addevent" : action ="events/new"); @page = places_path; break; //index places = new event
}
$('form').on('submit', function(event){
event.preventDefault();
$.ajax('$action', { //action as variable
type: 'GET',
data: $('form').serialize(),
dataType: 'json',
success: function(result){
redirect: @page
}
});
});
}
$(document).ready(main); `
答案 0 :(得分:0)
首先,将.js
文件重命名为.js.erb
以启用ruby表达式评估。 (有关详细信息,请查看this Rails Guides page。)
然后,你需要的东西看起来大致如此(未经过测试,但你可以得到这个想法):
<% url = MyRailsApp::Application.routes.url_helpers %>
switch(mapType) {
case "showevent" : action = "events/index"; page = "<%= url.events_path %>"; break; //index events
case "addplace" : action = "places/new"; page = "<%= url.new_place_path%>"; break;
case "addevent" : action ="events/new"; page = "<%= url.places_path %>"; break; //index places = new event
}
// ... use the 'action' and 'page' variables as you need here.
祝你好运。
编辑:添加了url
变量。您仍然可以使用js-routes
答案 1 :(得分:0)
好的,所以我在这里,我的更新代码js-rails无法正常工作,所以我在SO Link_to for static pages上转过这篇文章 - 实际上这是Zajo的提示,所以这里&#39 ; s我的完整代码`var main = function(){
function proceed(mapType){
var action,page;
switch(mapType) {
case "showevent" : action = "events/index"; page = "<%= url.events_path %>" ; break; //index events
case "addplace" : action = "places/new"; page = "<%= url.new_place_path %>"; break;
case "addevent" : action = "events/new"; page = "<%= url.places_path %>"; break; //index places = new event(to je v podstate len form)
// var event_path = "<%#= CGI.unescape url.event_path('{event_id}') %>";
}
$('form').on('submit', function(event){
event.preventDefault();
$.ajax({
type: 'GET',
url: action,
data: $('form').serialize(),
dataType: 'json',
success: function(result){
redirect: page // alebo solve in controller
}
});
});
}
}
(文档)$。就绪(主); `但同时出现的另一个问题是链接静态页面Link_to for static pages,这也是(high_voltage gem没有在视图中生成页面文件夹)adn第二个答案也没有工作