-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Render Rabl templates from view (in order to bootstrap a backbone.js model) #42
Comments
Just render the rabl template:
|
Awesome! That's super helpful! On Fri, Jul 1, 2011 at 9:04 AM, spoptchev <
|
@scottmessinger Can you confirm this works and if you have run into any other issues? I believe this is how I am doing it in my backbone project as well. Perhaps there should be a guide on the RABL wiki combining backbone and RABL. |
Is it also possible to set instance variables for the partial to access using this technique (e.g. for object/collection). Cheers. |
It seems like you probably need a way to set the object, perhaps with the :object option to render. I'm trying to do this as well, but instead I'm trying to embed the JSON version of object A inside the HTML version of object B. There doesn't appear to be a clean way to do this right now. I think the partial() function in RABL is almost exactly what is needed here. |
Yeah, still trying to figure out the best way to expose this functionality in RABL... |
To answer my own question somewhat, this works:
This is not as clean as it could be, but this is 99% of the way there and it works. |
Interesting, that isn't necessarily a bad approach. Thanks for spelling that out. Added that post and a link back to here to the Backbone Integration wiki page as a placeholder. I'd love to flesh out this guide more in the future. |
Well, I see two possibly cleanups:
I'm not sure if #2 is possible without Rails changes though. |
I am closing this for now because the solution you proposed is good enough |
Thanks for all the help with this! |
Making the rabl file a partial breaks default functionality. This seems to be the only way around it:
|
I'm confused what you're suggesting here. Your format.json suggests that you're calling this in a controller. Partials are meant for use from views, not controllers. |
Exactly, and if I make the rabl file a partial, I can't use the same file for both backbone and controller without this hack. |
But this is precisely what partials are for. How is this any different from how html views work? It just so happens with JSON/RABL, unlike with HTML, that a partial can look just like a full view for the same object, but that doesn't mean the abstractions should be any different. If you want to have a JSON/RABL partial that can be embedded in other views, you create a partial. |
Hey, When I try to do this, the best I can achieve is having "#<Rabl::Engine:0x007fad09cc7320>" rendered into my view. Is there a way around this? |
example? |
When you try what specifically? can you gist it? |
If I try this: https://gist.github.com/1297264 I get this: https://gist.github.com/1297266 Looks like to_s being called on the Rabl instance itself. |
What does the rabl file look like? What's it named? |
rabl file is plates/_index.json.rabl It looks like this: https://gist.github.com/1297270 |
(I've tried switching object to plates as I plan to do eventually, but it doesn't seem to be getting far enough to choke on that) |
What are you expecting to happen here? You're passing in a collection of strings, but then expecting them to have attributes. |
I'm expecting it to barf when it sees stuff it can't render. I've also tried passing in a valid object that can render (when I access this through a controller method), but I get the same "#Rabl::Engine:0x007fad0d4d0500" issue when I render inline. As far as I can tell, it's not trying to render the .rabl template at all. |
It's possible the collection part is breaking here, I have code almost identical to this that works, but for some reason I'm doing map{ render(...) }.join(','). Possibly I ran into the same problem. |
Interesting, are you on Rails 3.1? Looks like a bug but not sure how to fix it. I remember trying this before and having it work alright in Rails 2.3. |
Works for me on Rails 3. On Tue, Oct 18, 2011 at 9:42 PM, Nathan Esquenazi <
|
I am on Rails 3.1, which might be the problem. @mschulkind: even if I empty the .rabl partial the same thing happens, so I don't think it's getting far enough for that to be an issue. |
I know they changed the way rendering works on 3.1 but I don't know how and I don't actually have any 3.1 projects. Just Padrino, Rails 2.3 and Rails 3. So if anyone can help investigate and fix this in a patch, I'd appreciate it. |
I agree with nesquena, it looks like the template handler wiring isn't quite right, which definitely points to some sort of Rails change. |
https://github.com/johnf/haml/commit/5cb9e77dcf685865c5b759e5c285d6dd64a4305c This looks very relevant. |
...and another just for reference. |
Ah...yes that is extremely relevant. Thanks for tracking that down, looks like they changed the template handler api completely... |
Is there likely to be a workaround for now? |
I'm looking into a patch for this currently. Need the fix anyway :) |
I think the only way forward is to basically either take the haml approach (and try to make one handler for 3/3.1) or split it up in https://github.com/nesquena/rabl/blob/master/lib/rabl/template.rb even further creating a separate handler for 3.1 following stevegraham/twilio-rb#12 example |
Ok, I've made some progress on this: It's nothing to do with the HAML bug. The problem is that render() in Engine is getting "html" in self.request_format... when it should be "json". A temporary fix looks like this: https://gist.github.com/1297460 Check out line 17 - if we manually force the format to "json" this works as expected. In the Rails 2 version, the format is being set using { :format => #{template.format.inspect} } format is a private method on ActionView::Template in Rails 3, so we can't do this anymore. Thoughts? |
Ok, to be ultra-specific, something has changed between Rails 3.0 and Rails 3.1 to make this start returning "html"
It's getting "html" from
but the same thing doesn't occur in the Rails 3.0 test-case. Hmmm..... |
I've been trying to do as stated in the Wiki under Backbone integration but I can't get the view to render correctly:
I've tried to name the template
But only in another view in the I'm using Rails 3.2 and Rabl 0.7.0, is there any recent changes on this? is the recommended way to use Rabl::Renderer in views also? Edit: Oh and as suggested in #126
All I get is
|
Looks like there's relevant discussion going on at #126. |
Yes, I've tried the suggested methods there, but can't get it to work, only with this, and only if already in a template of the same folder:
I also need to name the file |
This has been my experience as well since well before rails 3.1 even. On Mon, Jul 30, 2012 at 12:08 PM, yeggeps <
|
Yeah, sorry this inline rendering stuff is so troublesome. Seems that what you need to do has changed on several occasions even just between Rails 3 and 3.1. If anyone can help update the wiki with the most relevant info, I'd appreciate it. Or if there's a simple way we can improve the template handler registration so this is less painful in the future. |
As you mentioned there's usually a way to pull it off. You can also |
So if using Rabl::Renderer or Rabl.render, is there a way to set the default view_path so I don't have to specify it every time? |
According to the readme on view_paths
it implies that you don't have to explicitly set |
In the backbone.js FAQ, Jeremy suggests bootstrapping a backbone.js model by using ERB and converting the object/collection to json:
http://documentcloud.github.com/backbone/#FAQ-bootstrap
How could I render a rabl template in the view instead of calling to_json on the object/collection?
The text was updated successfully, but these errors were encountered: