Using Fabricate on the front end in a .js.erb file.

17 views
Skip to first unread message

Brett Pontarelli

unread,
Sep 25, 2013, 3:32:42 PM9/25/13
to fabrica...@googlegroups.com
I'm running a proof of concept test to see if I can get Fabrication linked to my js.  The problem is `Fabricate` calls in the following test.js.erb file are returning the same values.  First, why would they always be the same?  and second, how can I change the (is is caching?) behavior to return a proper sample?  Note that the value doesn't change even if I edit the test.js.erb file, restart the rails server... I've tried lots of things.  Thanks.

In a test.js.erb file I have the following:

test = [
  <%= Fabricate.attributes_for(:item).to_json.gsub('\\','') + ',' %>
  <%= Fabricate.attributes_for(:item).to_json.gsub('\\','') + ',' %>
  <%= Fabricate.attributes_for(:item).to_json.gsub('\\','') %>
]

Which when served to `localhost:3000/assests/app/test.js` returns:

test = [
  {"name":"seven"}
  {"name":"seven"}
  {"name":"seven"}
]

The `item` Fabricator is:

Fabricator(:item) do
  name %w(one two three four five six seven eight nine ten).sample
end

Paul Elliott

unread,
Sep 26, 2013, 3:34:53 PM9/26/13
to fabrica...@googlegroups.com
You need to pass your list to it via a block so it is evaluated with every call to `Fabricate`. When you pass it that way, it is sampling your array once when the fabricator is loaded and storing that value.

```
Fabricator(:item) do
  name { %w(one two three four five six seven eight nine ten).sample }
end
```

-- Paul

--
You received this message because you are subscribed to the Google Groups "fabrication" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabricationge...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Brett Pontarelli

unread,
Sep 26, 2013, 7:14:59 PM9/26/13
to fabrica...@googlegroups.com
Yep!  That worked.  Not sure how I missed that ;/
Thanks.
Reply all
Reply to author
Forward
0 new messages