Comments (47)
Just render the rabl template:
<%= render(:file => 'path_to_rabl_template.json.rabl') %>
from rabl.
Awesome! That's super helpful!
On Fri, Jul 1, 2011 at 9:04 AM, spoptchev <
[email protected]>wrote:
Just render the rabl template:
<%= render(:file => 'path_to_rabl_template.json.rabl') %>
Reply to this email directly or view it on GitHub:
#42 (comment)
from rabl.
@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.
from rabl.
Is it also possible to set instance variables for the partial to access using this technique (e.g. for object/collection). Cheers.
from rabl.
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.
from rabl.
Yeah, still trying to figure out the best way to expose this functionality in RABL...
from rabl.
To answer my own question somewhat, this works:
- Create a partial for the object, this has to be named "app/views/model/_foo.json.rabl", note the leading underscore to indicate a partial.
- In your HTML view: render('model/foo.json.rabl', object: foo_object)
This is not as clean as it could be, but this is 99% of the way there and it works.
from rabl.
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.
from rabl.
Well, I see two possibly cleanups:
- RABL partial support should enforce the leading underscore to make this less confusing.
- It'd be nice if the line could be something more like "render(foo_object, :format => :json) " or even "render(json: foo_object)"
I'm not sure if #2 is possible without Rails changes though.
from rabl.
- I like RABL supporting partials without underscores because I want to re-use templates I am using in other places as a regular templates
- Pretty sure this can't be done without messing around in Rails rendering
I am closing this for now because the solution you proposed is good enough
from rabl.
Thanks for all the help with this!
from rabl.
Making the rabl file a partial breaks default functionality.
This seems to be the only way around it:
format.json {
render('lolcats/_index.json.rabl', :object => @cats)
}
from rabl.
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.
from rabl.
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.
from rabl.
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.
If you happen to also want a view that is nothing more than exactly what is in that partial, then just render the partial in that view.
from rabl.
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?
from rabl.
example?
from rabl.
When you try what specifically? can you gist it?
from rabl.
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.
from rabl.
What does the rabl file look like? What's it named?
from rabl.
rabl file is plates/_index.json.rabl
It looks like this: https://gist.github.com/1297270
from rabl.
(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)
from rabl.
What are you expecting to happen here?
You're passing in a collection of strings, but then expecting them to have attributes.
from rabl.
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.
from rabl.
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.
from rabl.
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.
from rabl.
Works for me on Rails 3.
On Tue, Oct 18, 2011 at 9:42 PM, Nathan Esquenazi <
[email protected]>wrote:
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.Reply to this email directly or view it on GitHub:
#42 (comment)
from rabl.
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.
from rabl.
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.
from rabl.
I agree with nesquena, it looks like the template handler wiring isn't quite right, which definitely points to some sort of Rails change.
from rabl.
https://github.com/johnf/haml/commit/5cb9e77dcf685865c5b759e5c285d6dd64a4305c
This looks very relevant.
from rabl.
...and another just for reference.
from rabl.
Ah...yes that is extremely relevant. Thanks for tracking that down, looks like they changed the template handler api completely...
from rabl.
Is there likely to be a workaround for now?
from rabl.
I'm looking into a patch for this currently. Need the fix anyway :)
from rabl.
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
from rabl.
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?
from rabl.
Ok, to be ultra-specific, something has changed between Rails 3.0 and Rails 3.1 to make this start returning "html"
def request_format
format = self.request_params.has_key?(:format) ? @_scope.params[:format] : nil
if request = @_scope.respond_to?(:request) && @_scope.request
format ||= request.format.to_sym.to_s if request.respond_to?(:format)
end
format && self.respond_to?("to_#{format}") ? format : "json"
end
It's getting "html" from
"request.format.to_sym.to_s"
but the same thing doesn't occur in the Rails 3.0 test-case. Hmmm.....
from rabl.
from rabl.
I've been trying to do as stated in the Wiki under Backbone integration but I can't get the view to render correctly:
render('restaurants/show', formats: :json, object: Restaurant.first)
I've tried to name the template _show.json.rabl
show.json.rabl
show.json.rabl
show.rabl
but without success, I did get this to work:
render('show.json.rabl', object: restaurant).html_safe
But only in another view in the restaurants
folder and I need to explictly do show.json.rabl
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
I've also tried:
render(file: 'restaurants/show', object: Restaurant.first, formats: :json).html_safe
All I get is
ActionView::MissingTemplate: Missing template restaurants/show with {:locale=>[:sv_SE], :formats=>[:json], :handlers=>[:erb, :builder, :coffee, :haml, :rabl, :eco]}. Searched in:
from rabl.
Looks like there's relevant discussion going on at #126.
from rabl.
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:
render('show.json.rabl', object: restaurant).html_safe
I also need to name the file _show.json.rabl
in order for this to work.
from rabl.
This has been my experience as well since well before rails 3.1 even.
On Mon, Jul 30, 2012 at 12:08 PM, yeggeps <
[email protected]
wrote:
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:render('show.json.rabl', object: restaurant).html_safe
I also need to name the file
_show.json.rabl
in order for this to
work.
Reply to this email directly or view it on GitHub:
#42 (comment)
from rabl.
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.
from rabl.
As you mentioned there's usually a way to pull it off. You can also Rabl.render
as you suggested which would be a simpler way to render inline templates if all else fails.
from rabl.
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?
from rabl.
According to the readme on view_paths
If view_paths is set to a path, this view path will be checked for every rabl template within your application. Add to this
path especially when including Rabl in an engine and using view paths within a another Rails app.
it implies that you don't have to explicitly set config.view_paths = ['app/views']
when using Rabl in a normal Rails app, is this correct? It's empty by default so I guess it needs to always be set?
from rabl.
Related Issues (20)
- RABL, Devise and sessions/create.rabl
- Drop riot? Unmaintained, broken on Ruby 2.5. HOT 2
- How to use rabl with swagger? HOT 2
- Tagging a new version, or use master branch? HOT 1
- Unable to properly render json for an simple array of objects HOT 1
- How do I render free form metadata HOT 1
- Migrating to Rails 5 - Rabl returning Active Record object
- Rspec renders rabl as string instead of json
- Not able to get the rendered result in Rails console HOT 1
- Rabl output values as keys
- Rabl partial lookup based on requested api namespace
- upgrading from 0.14.0 to 0.14.1 is a breaking change HOT 8
- Child removes node from response when the provided object is an empty array HOT 2
- Rabl not renders collection properly HOT 4
- Is nested map valid in RABL?
- N+1 issues HOT 1
- some problems to install rable on windows HOT 2
- Avoid playing with `Oj` shared `default_options` HOT 2
- rabl tests failing with rr 3.x and Ruby 3 HOT 12
- Rails 7 compatibility HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rabl.