I’m writing some functional tests for some drag-sort stuff, and I can’t
seem
to figure out how to pass an array in as part of my params in the xhr
method.
My code is the typical simple stuff that just reorders a set to match
the
order of the ids passed in:
def reorder_stuff @stuff = Stuff.find(params[:stuff_id]) @stuff.active_widgets.each do |widget|
widget.position = params[:widgets].index(widget.id.to_s) + 1
widget.save
end
render :nothing => true
end
When I try to simulate the post, I’m using the following:
I get a nil reference error when I run this, and I get the following
error
if I try to put the contents of params[:widgets] to stdout:
TypeError: can’t convert ActionController::Routing::PathSegment::Result
into
String
So, it seems that the array isn’t making it through routing, which makes
sense. But I’m at a bit of a loss as to how to get an array in any other
way
as part of params. Since it’s a hash, I can’t pass it in the same way
that
it arrives via the request: “widget[]=1&widget[]=3” since it would have
duplicate keys.
Thanks for the idea! It kind of works, and I actually get a hash instead
of
it getting mangled by routing.
Unfortunately I need it as an array so I can call index on it to get the
correct order… I could change the implementation, but then I’m
basically
writing it one way for the test and one way for the real world.
Thanks for the help! Any other ideas? I’ve spent all afternoon on
this,
and I’m sure I’m missing something really obvious…
Thanks for the idea! It kind of works, and I actually get a hash instead of
it getting mangled by routing.
Unfortunately I need it as an array so I can call index on it to get the
correct order… I could change the implementation, but then I’m basically
writing it one way for the test and one way for the real world.
<% @stuff.active_widgets.each do |w| %>
<%= render :partial => "account/stuff/widget", :locals => { "w" =>
w
} %>
Is everyone calling partials out of views as a twisted way to
functionalize
a block of stuff that we don’t want to type inline into long blocks?
I only use them when a view and AJAX will re-create the same stretch of
HTML. I use Builder::XmlMarkup to create lots of little functions that
create the intermediate HTML.
I can’t help with the sortable list because I’m only up to page 27 in
/Rails
Recipes/, and I can’t tell if the book explains exactly how all the
monotonically numbered IDs in the HTML list get transferred into the
AJAX
parameters…
to get params[:attribute] in as an array of values.
The sortable list itself works just fine, so my issue isn’t there…
It’s
how to functionally test it because I can’t see how to pass an array
parameter into the controller via xhr because an array passed in gets
mangled by the routing…
And yeah, I use partials because I re-render that particular bunch all
over
the place. It’s more DRY.
The request that makes it to Rails looks like:
“albums%5B%5D=3&albums%5B%5D=1&=", or "albums[]=3&albums[]=1&=”. It’s
the
same thing you do with a HABTM relationship managed via a select tag.
But,
I’ve just never actually tried to test this before.
I walked away from this one and came back a few days later… I thought
I’d
check the ActionPack test code to see what they do, and in cgi_test I
found
the answer…
When you pass in variables, don’t pass in integers: