【发布时间】:2014-06-10 13:26:20
【问题描述】:
提前感谢您的帮助,这个问题困扰了我好几天。
我在我的 rails 应用程序中有一个视图,它会随机化西班牙语中的一个单词,同时用英语呈现相同的单词。示例:
狗
操作
用户可以来回切换西班牙语单词的字母以“创建”正确的单词,在上面的示例中为“perro”。然后用户单击一个按钮提交答案,我的 javascript 正确地提醒数组“perro”或用户生成的任何内容。我的目标是在用户单击下一个按钮后将此 javascript 数组(以下代码中的 sortedIDs)传递回我的 rails 'lang' 控制器。下面的 ajax 代码似乎没有这样做,因为当我尝试使用下面的单行代码访问控制器中的数组时,我得到一个 nil 错误:
data = params[:order].split(',')
我的 javascript 代码如下所示:
$(function() {
$(" #sortable ").sortable({axis: "x", containment: "window"});
$( ".clicked" ).click( function() {
var sortedIDs = $( "#sortable" ).sortable( "toArray", {attribute: 'custom-cl'} );
alert(sortedIDs);
var target = "http://localhost:3000/langs";
$.ajax({
type: 'get',
url: target + '?order='+sortedIDs.join(',') ,
dataType: 'script'
});
}); });
我的索引视图如下所示:
<% provide(:title, 'Quiz') %>
<%= javascript_include_tag 'scramble' %>
<div class="center jumbotron" style="width: 100%; margin-left: auto; margin-right: auto;">
<% if @randomNumber == 0 %>
<h2> Word Scramble </h2>
<div class="well" style="background-color: #D8D8D8; width: 50%; margin-left: auto; margin-right: auto;">
<%= @spanishNotScrambled %>
</div>
<div class="panel panel-default" style="background-color: #CEECF5; width: 100%; margin-left: auto; margin-right: auto;">
<div class="panel-body">
<ul id="sortable">
<% @wordScramble.each do |letter| %>
<li class="ui-state-default" custom-cl="<%= letter %>" ><%= letter %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<% if @randomNumber == 1 %>
<h2> Some other exercise </h2>
<%= @test %>
<% end %>
<% if @index != 9 %>
<ul class="pager">
<div class="clicked"><%= link_to "Next", langs_path(:option => 'next2')%></div>
</ul>
<% else %>
<ul class="pager">
<div class="clicked"><%= link_to "Score Quiz!", langs_path(:option => 'scorequiz')%></div>
</ul>
<% end %>
</div>
编辑以添加 jsfiddle 示例:
Edit2 添加控制器代码:
class LangsController < ApplicationController
before_filter :signed_in_user, only:[:index, :show]
def index
@quiz = Lang.limit(10).offset(current_user.bookmark - 11)
@index = current_user.bookmark2
@randomNumber = rand(1)
which_button_clicked2
exercise_bank
data = params[:order].split(',')
end
private
def signed_in_user
redirect_to user_session_path, notice: "Please sign in." unless user_signed_in?
end
def which_button_clicked2
if params[:option] == "next2"
@index = @index + 1
current_user.bookmark2 = @index
current_user.save
end
if params[:option] == "scorequiz"
@index = 0
current_user.bookmark2 = @index
current_user.save
#redirect to page where show quiz results
end
end
def exercise_bank
if @randomNumber == 0 #execute word scramble exercise
@spanishNotScrambled = @quiz[@index].english_to_spanish
@wordScramble = @quiz[@index].english.split('').shuffle
end
if @randomNumber == 1 #execute some other exercise
@test = "Exercise 2"
end
end
end
【问题讨论】:
-
同时,你能解决这个问题吗?
-
还没有。计划在这个周末做更多事情,如果找到解决方案会更新。
-
能不能加个jsfiddle
-
params[order] 是否作为空数组传递。当 ajax 被触发时,你是否在控制台中检查了参数
-
我已经添加了 jsfiddle。当您在控制台中说检查参数时,我将如何做到这一点。你的意思是打开rails控制台吗?
标签: javascript jquery ruby-on-rails ajax arrays