问题
I've isolated my lack of knowledge down to this snipit I keep tweaking in irb. I'm trying to use jQuery's UI Sortable to send a post to the database, so far I can get it to send the update but it sends the entire array instead of each array item at a time. It's amazing that I have not found one post yet with someone trying Sinatra and Sortable. Hmmm
Hash and sinatra code
superslide = {"weee"=>["3", "4", "1", "2"]}
moo = Sort.all
moo.each do |o|
o.sortorder = superslide['weee']
puts o.sortorder
end
#Output
3412
3412
3412
3412
Above you can see it groups the arrays values into one line for each loop. I know there's a Ruby elementary thing i'm missing.
Below is the Rails equivalent I ultimately would like to clone to Sinatra, but little things screw up there as well, the
.index(book.id.to_s) + 1
throws an error, I guess since its a string, I tried to_i but it just edits the same group of numbers.
Rails code to translate
def sort
@books = Book.all
@books.each do |book|
book.position = params['book'].index(book.id.to_s) + 1
book.save
end
http://practiceovertheory.com/blog/2009/08/07/sortable-lists-with-jquery-in-rails/
I have also tested iterating the array in the hash like so, forgive the different names, I have a lot of test routes I can't make sence of now with test code.
slideNum.sortorder = params['slide'].each do |k,v|
k.each { |x| puts x }
end
jQuery Sortable if you need it
var foo = $( "#sortable" ).sortable({
update: function(event, ui){
$.ajax({
type: 'post',
// data: $('#books').sortable('serialize'),
data: foo.sortable("serialize",{key:'bunny[]'}),
dataType: 'script',
url: '/sort'
});
},
change: function(){console.log(foo.sortable("serialize",{key:'bunny[]'}))}
});
Needed output, I am assuming.
I want it to fire its update method and save the one hash=>key[]=>value So I guess the output would be, using an even simpler setup
superslide = {"weee"=>["3", "4", "1", "2"]}
superslide.each do |o|
ai = superslide['weee']
puts ai
puts "event"
end
Wanted output
3
event
4
event
2
event
1
event
Current not wanted output
Instead of
3
4
2
1
event
Update
In Sinatra i'm trying to save datafilenumber in this case like
datafilenumber.save = wee[0] # =1
datafilenumber.save = wee[1] # =2
etc…
updated test
superslide = {"weee"=>["3", "4", "1", "2"]}
superslide.each do |o|
ai = superslide['weee']
datafilenumber = ai.each {|x| "#{x}"}
puts "box + #{datafilenumber}"
end
#Outputs
box + 3412
Sinatra Route
Currently Outputs numbers and saves to database but its the same number, so all slides become that one value. Getting close!
post '/sort' do
sortall = Sort.all
slidevar = params['slide']
slidevar.each do |k,v|
k.each do |x|
sortall.update(:sortorder => x)
puts x
puts v.object_id
end
end
end
Testing with inspect and class
slidevar = params['slide']
puts params.inspect # {"slide"=>["3", "2", "1", "4"]}
puts params.class # Hash
puts slidevar.inspect # ["3", "2", "1", "4"]
puts slidevar.class # Array
回答1:
Is this what you are looking for? (based on your update)
superslide = {"weee"=>["3", "4", "1", "2"]}
superslide.each do |k,ai| # |key, value| i.e. |"weee", ["3", "4", "1", "2"]|
ai.each do |x| # will give you "3", "4", "1", "2" on iterations
datafilenumber.save = x # not sure what this was in your update
# do whatever other actions you need to do
end
end
It's still a little tough to tell what you want because your two updates are a little different, but I think what you need to make better use of iterators for your hash and the contained array. This gets you ready to create your "Wanted Output" you gave. Hope this helps.
Edit based on your most recent update. I think you meant to iterate over v
in line 5 of the new code. I'm assuming that slidevar
is the same hash you were dealing with before (superslide
).
Edit #2: Look closely at your inspection code. You are inspecting params
, a hash, and then params['slide']
, an array. However, your code treats params['slide']
as a hash itself. (Or I got confused throughout your sets of code. But either way this should work :)
post '/sort' do
sortall = Sort.all
# slidevar = params['slide']
params.each do |k,v| # with only one, it's equivalent to params['slide']
v.each do |x|
sortall.update(:sortorder => x)
puts x
puts v.object_id
end
end
end
Or you could do this:
post '/sort' do
sortall = Sort.all
slidevar = params['slide']
slidevar.each do |x|
sortall.update(:sortorder => x)
puts x
end
end
回答2:
I am not sure of what you want to do:
but for your desire output this code would work:
superslide = {"weee"=>["3", "4", "1", "2"]}
superslide.each do |o|
ai = superslide['weee']
ai.each {|x| puts "#{x}\n event"}
end
if I understand you this should work:
superslide["weee"].each {|x| datafilenumber.save = x}
It iterates over each elements in the array and saves them.
来源:https://stackoverflow.com/questions/9425096/sinatra-ruby-iterate-over-an-array-in-a-hash-to-send-jquery-sortable-to-database