Comments (10)
Hi,
Thanks for the options, just trying them now.
On the first option with the lambda, "m" is the model object, right - but what if we are using it in the scenario of "object false"...
Tried this, but it always excludes the field now :(
node(:message, :if => lambda { |m| @message.nil? }) { @message }
With option 2, I am probably just doing it wrong.
My helper is this:
module RablHelper
def node_if_present(name, &block)
node(name, { :if => lambda { |m| m.send(name) } }, &block)
end
end
And this in the view:
node_if_present(:message) { @message }
But get this error:
undefined method `node' for #<#<Class:0x106837fa0>:0x1068354a8>
Extracted source (around line #2):
1: object false
2: node(:request_id) { @request_id }
3: node(:status) { @status }
4: node_if_present(:message) { @message }
Rails.root: /Users/kimptoc/Documents/ruby/ecom/ecom1
Application Trace | Framework Trace | Full Trace
/Users/kimptoc/.rvm/gems/ree-1.8.7-2011.03@p-ecom1-rails3-wsse/bundler/gems/rabl-7f97b7e60aee/lib/rabl/engine.rb:20:in `render'
/Users/kimptoc/.rvm/gems/ree-1.8.7-2011.03@p-ecom1-rails3-wsse/bundler/gems/rabl-7f97b7e60aee/lib/rabl/engine.rb:162:in `send'
/Users/kimptoc/.rvm/gems/ree-1.8.7-2011.03@p-ecom1-rails3-wsse/bundler/gems/rabl-7f97b7e60aee/lib/rabl/engine.rb:162:in `method_missing'
(eval):4:in `render'
app/views/ws/new_quote.rabl:2:in `_app_views_ws_new_quote_rabl___1547417618_2202114200_0'
actionpack (3.0.7) lib/action_view/template.rb:135:in `send'
actionpack (3.0.7) lib/action_view/template.rb:135:in `render'
activesupport (3.0.7) lib/active_support/notifications.rb:54:in `instrument'
actionpack (3.0.7) lib/action_view/template.rb:127:in `render'
actionpack (3.0.7) lib/action_view/render/rendering.rb:59:in `_render_template'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.7) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from rabl.
Generally I like to include nil values so the key still exists consistently in the API but obviously it depends on the use case. The only way right now is using the if syntax to check. I could see wanting it globally but to me this seems more like a case by case setting...
node(:foo, :if => lambda { |m| m.foo }) { ... }
Keep in mind helpers work in rabl as well so you could add a shortcut in theory...a helper that returns a proc.
from rabl.
I just realized helpers open up a whole slew of extensions you could build for instance, in a helper you could theoretically do:
# app/helpers/rabl_helper.rb
def node_unless_nil(name, &block)
node(name, { :if => lambda { |m| m.send(name) } }, &block)
end
and then use that in your template:
node_unless_nil(:foo) { ... }
from rabl.
It looks like you have the logic reversed, try:
node(:message, :unless => lambda { |m| @message.nil? }) { @message }
Note in your example you said to display the message only if the message is nil which doesn't make sense.
As for the second example, you would need to include the helper directly into the template, just for fun try:
include RablHelpers
node_if_present(:message) { @message }
or to include special helpers in every template:
RABL::Engine.send(:include, RablHelpers)
I would probably just go with the direct "if" approach for now.
from rabl.
Hi,
Just tried this - the first, inline option, works now - I had been using ".present?" and tried switching to ".nil?" to see if it made a difference... guess I gave up too quick :(
Still cant get the helper to work.
- if I use the Engine.send option, its not finding RABL:: ... - I tried at the end of initializer and directly in the controller.
- If I include it in the template, I get undefined method "include"
Dont suppose its a new feature that I need to update the gem for?
Thanks,
Chris
from rabl.
Ok try this:
helper RablHelpers
node_if_present(:message) { @message }
and see if that works. I am aircoding these so it is likely my examples are just wrong in some cases. This one might work, let me know :)
from rabl.
Hi,
Getting closer - the helper is being loaded, but the call in the helper is failing :(
Thanks, Chris.
from rabl.
What about just:
node(:message) { @message } if @message
I am sure it is close with the helper, but I am not sure exactly what you are after for it to do, my version was more of an example. I am sure you could get it working, it just depends on what you want the method to do.
from rabl.
Thats simpler/clearer - nice. Thanks.
from rabl.
I realise this thread is very old, but it was the first one popping up on my google search "rabl exclude nils" so for future reference: I have created a pull request for excluding nil values globally: #514
from rabl.
Related Issues (20)
- 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
- access controller instance variables in representers
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.