Comments (10)
I never considered using a hash as the object, interesting idea. I always used a class (ORM) record as the object. I guess I don't see a reason a Hash couldn't be made to work but not surprising it doesn't. There are a few assumptions I made about the object right now and one of them is that the object responds to the "valid?" method and that it has dot notations to access attributes.
Here's how I would achieve what you want for now:
object false
node(:foo) { "bar" }
node(:pop) { 123 }
or if its a nested hash:
object false
node(:thing) { @thing }
I have to give this some thought, this is a use case I never considered supporting.
from rabl.
Also as for the undefined method
to_html' for #Rabl::Engine:0x1075c0730` it looks like you are trying to use RABL during an HTML request (not a json or xml request). You probably shoudn't allow the action to respond to HTML or have an alternate erb or haml template for that case...
from rabl.
Hi,
Thanks for the feedback - will try what you suggest.
I tried creating a local object like this:
class Thing
attr_accessor :foo, :pop
def initialize(a,b)
@foo =a
@pop =b
end
end
So the controller did this:
@thing2 = Thing.new("boo",987)
And template is this:
object @thing2
attributes :foo, :pop
Getting 'null' response (when I specify the format).
On the to_html stuff, yesterday I got away without it - not sure how. Wonder if there is a way to make json the default format.
Thanks,
Chris
from rabl.
Hi,
Just to say your suggestion with the "object false" does the job - and is clearer in my controller, so I prefer it - the hash was a contrived object to get the data to the view - so better to not have it :)
Thanks,
Chris
from rabl.
@kimptoc the to_html error comes from this change yesterday: ed9ec55 in which I detect the request format if theres no explicit format. I will have to fix it by disallowing html as the format I think and default to json.
As for the Thing record, try this for fun:
class Thing < Struct.new(:foo, :pop)
def initialize(h)
super *h.values_at(*self.class.members.map {|s| s.intern})
end
def valid?; true; end
end
and
@thing2 = Thing.new(:foo => "boo", :pop => 987)
Can you tell me if that works?
from rabl.
@kimptoc Okay, I am actually going to close this for now, I have never had the need to use a hash as the object. I don't think I will in the future either. In the case of constructing a response from scratch I would go with the object false approach :) If you or someone else finds this issue, I am not against the idea if someone else would like to patch it in with a pull request. I will fix the "to_html" issue tho soon. Thanks!
from rabl.
Hi,
The Thing suggestion works just fine and yes, please close it - was going to suggest that.
Regards,
Chris
from rabl.
@kimptoc Fixed the 'to_html' issue here, can you confirm? 7f97b7e
from rabl.
Hey @nesquena - perfect, works like a charm :)
Many thanks.
from rabl.
Excellent thanks man, pushing out a small gem point release.
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.